2

我在列表框中显示图像。我已将此列表框放在滚动查看器中。我正在使用两个重复按钮来移动列表框项目。我正在使用数据上下文绑定列表框。

问题:

如果我使用按钮移动图像并单击 lisbox 中的图像,它将移动到初始位置。

代码:

   <RepeatButton Click="rbtnLeft_Click" Name="rbtnLeft" Width="30" Height="30">
                <Image Source="Images/GeneralImages/search_right_arrow.jpg"></Image>
            </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#代码:

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);
    }
4

2 回答 2

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. 在 ListBox 中设置ScrollViewer.CanContentScroll="False"以防止这种情况发生。

于 2010-07-06T12:35:41.680 回答
1

您需要关闭 ListBox 内的内部 ScrollViewer。您可以通过重新模板化 lbGameImage 以完全删除 ScrollViewer 来做到这一点,但更快的方法(看起来像您尝试做的那样)是将 lbGameImage 上的两个 ScrollBarVisibility 设置都设置为“禁用”。隐藏意味着它们仍然处于活动状态并滚动内容,您只是看不到它们。

于 2010-07-06T13:36:45.830 回答