【发布时间】:2019-07-29 01:52:14
【问题描述】:
我有一个定义了 DataTemplate 的 ListBox。 ListBox 的 ItemSource (ListBoxItem) 是通过 ViewModel 提供的。
我希望在项目数超过 5 时显示 ListBox 的滚动查看器。如果有人可以帮助我,我会很高兴。
这是我的代码的一部分:
<ListBox VerticalAlignment="Stretch" Grid.Column="0"
ItemsSource="{Binding Path=Parts, Mode=OneWay}" SelectedIndex="{Binding CurrentPartIndex}"
Height="115" BorderThickness="1" BorderBrush="{DynamicResource {x:Static SystemColors.WindowTextBrush}}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" >
<Label Grid.Column="0" Content="{Binding CurrentLabel, Mode=OneWay}" MinWidth="150" Width="auto" VerticalAlignment="Stretch"/>
<Label Grid.Column="1" Content="{Binding ItemNumber, Mode=OneWay}" MinWidth="50" VerticalAlignment="Stretch"/>
<Label Grid.Column="2" Content="{Binding Cut, Mode=OneWay}" MinWidth="50" VerticalAlignment="Stretch"/>
<Label Grid.Column="3" Content="{Binding Material, Mode=OneWay}" MinWidth="100" VerticalAlignment="Stretch"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
【问题讨论】:
-
尝试将水平和垂直 ScollbarVisibility 设置为自动。并玩弄控制高度,这样 5 个项目就不会导致滚动条出现。
-
我会为 ScrollViewer 实现一个 IValueConverter,它将项目计数转换为 HorizontalalScrollBarVisibility,对于值 > 5 返回 Visible,否则为隐藏。
-
@PrateekShrivastava 我尝试过这种方式,但这不是一般的方式,因为它取决于分辨率。不同PC的变化