Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Please see Update at bottom of post

I'm writing a WPF app using .Net 4.5

I have a listbox displaying movie info, the xaml is shown below.

<ListBox Margin="10, 5, 10, 10"
             ItemsSource="{Binding SearchResponse.Results}"
             HorizontalAlignment="Stretch"
             VerticalAlignment="Stretch">
        <ListBox.ItemContainerStyle>
            <Style TargetType="ListBoxItem">
                <EventSetter Event="MouseDoubleClick"
                             Handler="SearchResult_OnMouseDoubleClick">
                </EventSetter>
            </Style>
        </ListBox.ItemContainerStyle>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Border BorderBrush="#3e3e42"
                        BorderThickness="1"
                        CornerRadius="5"
                        Background="#242424"
                        Padding="10">
                    <StackPanel Orientation="Horizontal">
                        <Image Source="{Binding ThumbnailPath}"
                               Width="{Binding RelativeSource={RelativeSource FindAncestor,
                                AncestorType={x:Type Window}},
                                Path=DataContext.ThumbnailWidth}"
                               Height="{Binding RelativeSource={RelativeSource FindAncestor,
                                AncestorType={x:Type Window}},
                                Path=DataContext.ThumbnailHeight}"
                               Margin="0,0,10,0" />
                        <StackPanel TextBlock.FontFamily="Segoe WP"
                                    TextBlock.FontSize="14"
                                    TextBlock.Foreground="WhiteSmoke"
                                    Width="300">
                            <TextBlock Text="{Binding Title}"
                                       TextTrimming="CharacterEllipsis" />
                            <TextBlock Text="{Binding Description}"
                                       TextTrimming="CharacterEllipsis"
                                       TextWrapping="Wrap"/>
                            <TextBlock Text="{Binding Path}"
                                       TextTrimming="CharacterEllipsis"/>
                        </StackPanel>
                    </StackPanel>
                </Border>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

Every thumbnail (Image) in the list will have the same width and height (either small, medium or large). For the window I have SizeToContent=Width so the window will resize itself when the thumbnail size is changed.

The problem is this: When I scroll the listbox using the mouse wheel the listbox width changes seemingly randomly and causes the window to increase in width. If I scroll using the scrollbar up and down buttons this doesn't happen.

I've debugged the ActualWidth of all of the elements in the listbox item data template (text blocks, stack panels, border, etc.) and they all stay at the expected fixed width yet debugging the width of the listbox itself shows that it changes width randomly.

Here are two screenshots. The first shows the correct layout and the second shows the layout after scrolling the listbox with the mouse wheel.

Correct layout

After scrolling, listbox gets wider!

So, why does the listbox ActualWidth change when the items contained within do not?

Update: Some further investigation suggests that this behaviour is being caused by the fact that the images I am using for the thumbnail are larger than the thumbnail display size and are resized by the image control. It appears that the listbox is (sometimes) resized according to the original image size rather than the smaller resized image.

Anyone have any suggestions to overcome this?

share|improve this question

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.