是否可以限制列表框显示的行数? 例如。假设我有一个包含100个项目的ItemSource,但我只希望我的列表框高达10个项目。
答案 0 :(得分:1)
如果您希望ListBox
仅适合 10个项目,必须滚动其余项目,您只需将ListBoxItem
s的高度设置为高度ListBox
除以10。
如果您想允许ListBox
调整大小,则必须在每次调整大小事件时动态调整ListBoxItem
高度。
静态示例:
<ListBox Height="500">
<ListBox.Resources>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Height" Value="50"/>
</Style>
</ListBox.Resources>
<ListBoxItem>One</ListBoxItem>
<ListBoxItem>Two</ListBoxItem>
<ListBoxItem>Three</ListBoxItem>
<ListBoxItem>Four</ListBoxItem>
<ListBoxItem>Five</ListBoxItem>
<ListBoxItem>Six</ListBoxItem>
<ListBoxItem>Seven</ListBoxItem>
<ListBoxItem>Eight</ListBoxItem>
<ListBoxItem>Nine</ListBoxItem>
<ListBoxItem>Ten</ListBoxItem>
<ListBoxItem>Eleven</ListBoxItem>
<ListBoxItem>Twelve</ListBoxItem>
<ListBoxItem>Thirteen</ListBoxItem>
<ListBoxItem>Fourteen</ListBoxItem>
<ListBoxItem>Fifteen</ListBoxItem>
<!-- etc. -->
</ListBox>
答案 1 :(得分:-1)
这对我有用:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
// Set height of list box
this.listbox.Height = (this.listbox.ActualHeight / this.listbox.Items.Count) * 10;
}
将此附加到您的控件的Loaded
事件中(否则未设置ActualHeight
)。