Scrollviewer Problem wpf
I am displaying images inside the listbox. i have placed this listbox inside the scrollviewer. I am using two repeat button to move the listbox items. I am binding the listbox using datacontext.
Problem:
If i move the images using the button and click on the image in the lisbox it moves to the initial position.
Code:
<RepeatButton Click="rbtnLeft_Click" Name="rbtnLeft" Width="30" Height="30">
<Image Source="Images/GeneralImages/search_right_arrow.jpg"></I开发者_高级运维mage>
</RepeatButton>
<Grid x:Name="grid" Width="666" HorizontalAlignment="Left">
<ScrollViewer Grid.Row="1" Name="svGame"
VerticalScrollBarVisibility="Hidden"
HorizontalScrollBarVisibility="Hidden" >
<ListBox ClipToBounds="True" Name="lbGameImage" Width="Auto" SelectionChanged="lbGameImage_SelectionChanged" ItemsSource="{Binding}" ItemsPanel="{DynamicResource iptListBox}" ItemContainerStyle="{DynamicResource ListBoxItemStyle}"
ScrollViewer.VerticalScrollBarVisibility="Hidden"
ScrollViewer.HorizontalScrollBarVisibility="Hidden"/>
</ScrollViewer>
</Grid>
<RepeatButton Click="rbtnRight_Click" Name="rbtnRight" Width="30" Height="30">
<Image Source="Images/GeneralImages/search_left_arrow.jpg"></Image>
</RepeatButton>
c# Code:
private void rbtnLeft_Click(object sender, RoutedEventArgs e)
{
svGame.ScrollToHorizontalOffset(svGame.HorizontalOffset + 5);
}
private void rbtnRight_Click(object sender, RoutedEventArgs e)
{
svGame.ScrollToHorizontalOffset(svGame.HorizontalOffset - 5);
}
The problem is that the ListBox thinks it owns the ScrollViewer, so whenever the selection changes it sets the offset back to what it wants. Set ScrollViewer.CanContentScroll="False"
in the ListBox to prevent this.
You need to turn off the internal ScrollViewer inside the ListBox. You could do it by re-templating lbGameImage to remove the ScrollViewer completely but the quicker way (which it looks like you tried to do) is to set both of the ScrollBarVisibility settings on lbGameImage to "Disabled". Hidden means that they're still active and scrolling the content, you just can't see them.
精彩评论