WPF ItemsControl 仅显示第一项

WPF ItemsControl display only first item

我在 XAML 中定义了 ItemsControl。

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"></RowDefinition>
        <RowDefinition Height="Auto"></RowDefinition>
        <RowDefinition Height="*"></RowDefinition>
        <RowDefinition Height="*"></RowDefinition>
    </Grid.RowDefinitions>

    ...

    <StackPanel Orientation="Horizontal" Grid.Row="2" Margin="5" MaxHeight="160">

        <ItemsControl Name="FilterItemsControl" ItemsSource="{Binding Filters}" >
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <GroupBox Header="{Binding Path=Key}" Margin="10,0,0,5" MaxWidth="150">
                        <ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
                            <ItemsControl ItemsSource="{Binding Path=Value}" >
                                <ItemsControl.ItemTemplate>
                                    <DataTemplate>
                                        <CheckBox  Content="{Binding Path=Name}" IsChecked="{Binding Path=IsChecked}" Margin="1"/>
                                    </DataTemplate>
                                </ItemsControl.ItemTemplate>
                            </ItemsControl>
                        </ScrollViewer>
                    </GroupBox>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>

    </StackPanel>

    ...

</Grid>

视图模型:

public Dictionary<string, ObservableCollection<CheckableItem>> Filters
    {
        get 
        { 
            return filters; 
        }
        set
        {
            filters = value;
            OnPropertyChanged("Filters");
        }
    }

CheckableItem 的简单 class:

public class CheckableItem
{
    private long id;
    private bool isChecked;
    private string name;

    public long Id
    {
        get { return id; }
        set { id = value; }
    }

    public string Name
    {
        get { return name; }
        set { name = value; }
    }

    public bool IsChecked
    {
        get { return isChecked; }
        set { isChecked = value; }
    }
}

Filters 字典已正确填充到 ViewModel 中。 Filters.Count 也大于一。 我想以水平方向显示字典中的每个 KeyValuePair,但只显示字典中的第一项,为什么?

另外,水平方向的代码可以吗?

ScrollViewer 放在 StackPanel 中是个坏主意,因为 StackPanel 用无限 space 测量其 children:

如果您希望 ItemsControl 水平布置项目,您应该将其 ItemsPanel 设置为水平 StackPanel

试试这个:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"></RowDefinition>
        <RowDefinition Height="Auto"></RowDefinition>
        <RowDefinition Height="*"></RowDefinition>
        <RowDefinition Height="*"></RowDefinition>
    </Grid.RowDefinitions>

    <ScrollViewer Grid.Row="2" Margin="5" ScrollViewer.HorizontalScrollBarVisibility="Auto">
        <ItemsControl Name="FilterItemsControl" ItemsSource="{Binding Filters}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal" />
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <GroupBox Header="{Binding Path=Key}" Margin="10,0,0,5" MaxWidth="150">
                        <ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
                            <ItemsControl ItemsSource="{Binding Path=Value}" >
                                <ItemsControl.ItemTemplate>
                                    <DataTemplate>
                                        <CheckBox  Content="{Binding Path=Name}" IsChecked="{Binding Path=IsChecked}" Margin="1"/>
                                    </DataTemplate>
                                </ItemsControl.ItemTemplate>
                            </ItemsControl>
                        </ScrollViewer>
                    </GroupBox>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </ScrollViewer>
</Grid>