1

所以我有一个列表,其中包含项目。现在它们是图片的缩略图。我希望这个列表绑定到后面代码中的一个不断变化的列表,所以我使用了一个列表框。但是,我需要这个盒子水平流动。所以它的样式是 StackPanel。最后,我想要按钮来控制滚动,而不是滚动条。那是不起作用的部分这是一个代码示例:

<UserControl x:Class="TestBench.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">

<UserControl.Resources>

    <Style x:Key="StackHorz" TargetType="ListBox">
        <Style.Setters>
            <Setter Property="ItemsPanel">
                <Setter.Value>
                    <ItemsPanelTemplate>
                        <StackPanel Orientation="Horizontal" VerticalAlignment="Top" Background="AliceBlue" />
                    </ItemsPanelTemplate>
                </Setter.Value>
            </Setter>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ListBox">
                        <ScrollViewer BorderBrush="DarkGreen" BorderThickness="2" VerticalScrollBarVisibility="Disabled"  HorizontalScrollBarVisibility="Disabled">
                            <ItemsPresenter />
                        </ScrollViewer>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style.Setters>
    </Style>

</UserControl.Resources>

<Grid x:Name="LayoutRoot" Background="White">
    <Button x:Name="_Next" Content="NEXT" Height="20" Width="40" VerticalAlignment="Bottom" HorizontalAlignment="Right"/>
    <Button x:Name="_Prev" Content="PREV" Height="20" Width="40" VerticalAlignment="Bottom" HorizontalAlignment="Left"/>
    <ListBox x:Name="TestList" Height="100" Width="800" VerticalAlignment="Top">
        ...Insert ListItems...
    </ListBox>
</Grid>

在此示例中,列表框未绑定,但我确实需要能够设置 ItemsSource={Binding Content}。我试过的代码是:

namespace TestBench

{ 公共部分类 MainPage : UserControl { 公共 MainPage() { InitializeComponent();

        TestList.Style = this.Resources["StackHorzTop"] as Style;
        _Next.Click += new RoutedEventHandler(_Next_Click);
        _Prev.Click += new RoutedEventHandler(_Prev_Click);
    }

    void _Prev_Click(object sender, RoutedEventArgs e)
    {
        TestList.ScrollIntoView(TestList.Items[0]);
    }

    void _Next_Click(object sender, RoutedEventArgs e)
    {
        TestList.ScrollIntoView(TestList.Items[TestList.Items.Count - 1]);
    }
}

}

但是 ScrollIntoView 什么也不做。我还尝试将 ScrollViewer 作为列表框的 VisualTreeHelper.GetChild(),但使用 ScrollToHorizo​​ntalOffset() 滚动也无济于事。

我知道这是一种奇怪的设置方式,但我需要所有 3 个功能(绑定、水平方向、无滚动条和按钮滚动)。有人知道我在这个问题上哪里出错了吗?

提前致谢,

图表。

4

1 回答 1

0

也许您可以尝试将列表框放在 ScrollViewer 中并设置 ScrollViewer 的样式,以便滚动条不可见(仅按钮)。

可以在此处找到有关 ScrollViewer 的模板部分的更多信息。

于 2010-10-26T06:39:26.257 回答