WPF ItemsControl仅显示第一项

时间:2017-03-01 16:45:49

标签: c# wpf datagrid itemscontrol

我在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的简单类:

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; }
    }
}

在ViewModel中正确填充了过滤器字典。 Filters.Count也大于一。 我希望在水平方向上显示字典中的每个KeyValuePair,但只显示字典中的第一项,为什么?

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

1 个答案:

答案 0 :(得分:2)

ScrollViewer置于StackPanel内是一个坏主意,因为StackPanel会测量其子女的无限空间:

Horizontal scroll for stackpanel doesn't work

如果您希望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>