2

我想要什么:让该死的水平滚动条出现。我将对其进行一些编辑,以便适合应用程序的其余样式方案,但不会太多。

我有的

这是到目前为止列表框的代码。一切运行完美,除了滚动条不出现。您可能会说...“好吧,您在任何地方都没有滚动查看器”,但是我尝试在许多地方插入滚动查看器,但仍然没有运气。

列表框代码:

<ListBox ItemsSource="{Binding Items}" ItemTemplate="{StaticResource itemsdatatemplate}" Background="{x:Null}" BorderBrush="{x:Null}" Foreground="{x:Null}" ItemsPanel="{StaticResource HorizontalListBoxTemplate}" ItemContainerStyle="{StaticResource TransparentListBox}" VerticalAlignment="Center" SelectedItem="{Binding SelectedItem, Mode=TwoWay}" />

'TransparentListBox'(关闭选定的背景颜色):

<Style x:Key="TransparentListBox" TargetType="ListBoxItem">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ListBoxItem">
                <Grid>
                    <Border x:Name="HoverBorderBackgroundBrush" BorderThickness="1" Margin="0,0,25,0" Background="Transparent"/>
                    <Border x:Name="SelectedBorderBackgroundBrush" BorderThickness="1" Margin="0,0,25,0" Background="Transparent"/>
                        <ContentPresenter></ContentPresenter>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

水平列表框(使列表框水平,而不是标准垂直)

<ItemsPanelTemplate x:Key="HorizontalListBoxTemplate">
    <StackPanel Orientation="Horizontal">
    </StackPanel>
</ItemsPanelTemplate>

数据模板(实际显示项目)

<DataTemplate x:Key="itemsdatatemplate">
        <local:ListItemControl HorizontalAlignment="Left" VerticalAlignment="Top" DataContext="{Binding}"/>
</DataTemplate>

我有一种感觉,这将是一个简单的添加,但在此先感谢。

更新

看起来滚动条现在确实出现了:

    <Style x:Key="ScrollingListBox" TargetType="ListBox">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>
                    <Grid>
                        <ScrollViewer HorizontalScrollBarVisibility="Visible">
                            <ItemsPresenter></ItemsPresenter>
                        </ScrollViewer>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

但它们没有相应地发挥作用。他们觉得……破碎了。但是,如果要定义网格的静态宽度(比如 300),那么 ScrollViewer 就可以完美运行。现在我有一个完全流畅的布局(意思是拉伸填充),这对于滚动查看器来说是不可接受的吗?

4

1 回答 1

0

当您创建自己的模板时,您必须在其中定义 ScrollViewer 并使用 ItemPresenter 而不是 ContentPresenter。

<Style x:Key="TransparentListBox" TargetType="ListBoxItem">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ListBoxItem">
                <Grid>
                    <Border x:Name="HoverBorderBackgroundBrush" BorderThickness="1" Margin="0,0,25,0" Background="Transparent"/>
                    <Border x:Name="SelectedBorderBackgroundBrush" BorderThickness="1" Margin="0,0,25,0" Background="Transparent"/>
                        <ScrollViewer x:Name="ScrollViewer" HorizontalScrollBarVisibility="Visible">
                            <ItemsPresenter />
                        </ScrollViewer>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
于 2011-03-23T22:31:38.127 回答