Is it possible to limit the number of rows a listbox show? Eg. let´s say I have a ItemSource with 100 items, but I only want my listbox to be 10 items high.
是否可以限制列表框显示的行数?如。让´s说我有一个ItemSource 100件,但我只希望我的列表框高10项。
2 个解决方案
#1
-1
This worked for me:
这工作对我来说:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
// Set height of list box
this.listbox.Height = (this.listbox.ActualHeight / this.listbox.Items.Count) * 10;
}
Attach this to the Loaded
Event of your control (otherwise ActualHeight
is not set).
将此附加到控件的已加载事件(否则未设置实际高度)。
#2
1
If you want your ListBox
to only fit 10 items, having to scroll for the rest, you can simply set the height of ListBoxItem
s to be the height of the ListBox
divided by 10.
如果你希望你的列表框只能容纳10个条目,那么你可以简单地将列表框条目的高度设置为列表框的高度除以10。
If you want to allow the ListBox
to resize, you will have to adjust the ListBoxItem
height dynamically on each resize event.
如果您想允许列表框调整大小,您必须在每个调整大小事件上动态调整列表框项的高度。
Static example:
静态的例子:
<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
This worked for me:
这工作对我来说:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
// Set height of list box
this.listbox.Height = (this.listbox.ActualHeight / this.listbox.Items.Count) * 10;
}
Attach this to the Loaded
Event of your control (otherwise ActualHeight
is not set).
将此附加到控件的已加载事件(否则未设置实际高度)。
#2
1
If you want your ListBox
to only fit 10 items, having to scroll for the rest, you can simply set the height of ListBoxItem
s to be the height of the ListBox
divided by 10.
如果你希望你的列表框只能容纳10个条目,那么你可以简单地将列表框条目的高度设置为列表框的高度除以10。
If you want to allow the ListBox
to resize, you will have to adjust the ListBoxItem
height dynamically on each resize event.
如果您想允许列表框调整大小,您必须在每个调整大小事件上动态调整列表框项的高度。
Static example:
静态的例子:
<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>