常に先頭5個しか出さないのであれば、これでいいのでは?(ViewModelでTake(5)
すればいいだけな気もしますが^^;
xml
1<ListBox>
2 <ListBox.ItemsPanel>
3 <ItemsPanelTemplate>
4 <UniformGrid Columns="1" />
5 </ItemsPanelTemplate>
6 </ListBox.ItemsPanel>
7 <ListBoxItem Content="{Binding [0]}" />
8 <ListBoxItem Content="{Binding [1]}" />
9 <ListBoxItem Content="{Binding [2]}" />
10 <ListBoxItem Content="{Binding [3]}" />
11 <ListBoxItem Content="{Binding [4]}" />
12</ListBox>
より一般的な「表示行数の固定」(高さによらず常に同じ個数が見える)は、高さから計算するしかなさそうです。
xml
1<Window
2 x:Class="Q49265.MainWindow"
3 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
4 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
5 xmlns:math="http://hexinnovation.com/math"
6 Width="800"
7 Height="450"
8 ThemeMode="System">
9 <Window.Resources>
10 <ControlTemplate x:Key="ListBoxTemplate">
11 <UniformGrid Columns="1" IsItemsHost="True" Rows="5" />
12 </ControlTemplate>
13 <math:MathConverter x:Key="Math" />
14 </Window.Resources>
15
16 <DockPanel>
17 <UniformGrid DockPanel.Dock="Top" Rows="1">
18 <Button HorizontalAlignment="Stretch" Click="Button_Click" Content="ten thousand" />
19 <Button HorizontalAlignment="Stretch" Click="Button_Click_1" Content="two" />
20 </UniformGrid>
21
22 <UniformGrid Rows="1">
23 <!--<ListBox ItemsSource="{Binding}" Template="{StaticResource ListBoxTemplate}">
24 <ListBox.ItemContainerStyle>
25 <Style BasedOn="{StaticResource {x:Type ListBoxItem}}" TargetType="ListBoxItem">
26 <Setter Property="VerticalContentAlignment" Value="Stretch" />
27 <Setter Property="HorizontalContentAlignment" Value="Center" />
28 </Style>
29 </ListBox.ItemContainerStyle>
30 </ListBox>-->
31
32 <ListBox>
33 <ListBox.ItemsPanel>
34 <ItemsPanelTemplate>
35 <UniformGrid Columns="1" />
36 </ItemsPanelTemplate>
37 </ListBox.ItemsPanel>
38 <!-- 5個以下があり得るなら -->
39 <ListBox.ItemContainerStyle>
40 <Style BasedOn="{StaticResource {x:Type ListBoxItem}}" TargetType="ListBoxItem">
41 <Setter Property="Padding" Value="12,0" />
42 <Style.Triggers>
43 <Trigger Property="Content" Value="{x:Null}">
44 <Setter Property="Visibility" Value="Hidden" />
45 </Trigger>
46 </Style.Triggers>
47 </Style>
48 </ListBox.ItemContainerStyle>
49 <ListBoxItem Content="{Binding [0]}" />
50 <ListBoxItem Content="{Binding [1]}" />
51 <ListBoxItem Content="{Binding [2]}" />
52 <ListBoxItem Content="{Binding [3]}" />
53 <ListBoxItem Content="{Binding [4]}" />
54 </ListBox>
55
56 <!-- より一般的な「表示行数の固定」(高さによらず常に同じ個数が見える) -->
57 <ListBox x:Name="listBox" ItemsSource="{Binding}">
58 <ListBox.ItemContainerStyle>
59 <Style BasedOn="{StaticResource {x:Type ListBoxItem}}" TargetType="ListBoxItem">
60 <Setter Property="Height" Value="{Binding ActualHeight, ElementName=listBox, ConverterParameter=x/5, Converter={StaticResource Math}}" />
61 <Setter Property="Padding" Value="12,0" />
62 </Style>
63 </ListBox.ItemContainerStyle>
64 </ListBox>
65 </UniformGrid>
66 </DockPanel>
67</Window>
cs
1using System.Windows;
2
3namespace Q49265;
4
5public partial class MainWindow : Window
6{
7 public MainWindow() => InitializeComponent();
8
9 private void Button_Click(object sender, RoutedEventArgs e)
10 => DataContext = Enumerable.Range(1, 10000);
11
12 private void Button_Click_1(object sender, RoutedEventArgs e)
13 => DataContext = Enumerable.Range(1, 2);
14}
NuGet Gallery | MathConverter

バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/09/26 07:49