在ViewModel中使用ItemsSource对ItemsControl进行分组。

问题描述 投票:1回答:2

我已经定义了 ItemsControl 这样的。

<ItemsControl Grid.Row="2" Style="{StaticResource SellingDashboardToDosList}"
              BorderThickness="1" Background="#C7E8F8" HorizontalAlignment="Stretch" 
              ItemsSource="{Binding Path=ToDoList}">
    <ItemsControl.GroupStyle>
        <GroupStyle>
            <GroupStyle.ContainerStyle>
                <Style TargetType="GroupItem">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="GroupItem">
                                <GroupBox Header="{Binding Path=Model.TodoType}" >
                                    <ItemsPresenter />
                                </GroupBox>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </GroupStyle.ContainerStyle>
        </GroupStyle>
    </ItemsControl.GroupStyle>
</ItemsControl>

The ItemsSource 是一个 SynchronisedObservableCollection<T>ViewModel. 但是这个XAML并没有产生任何分组。我想,我应该以某种方式指定 ItemsSource 是可以分组的。但我应该在哪里指定它呢?

如果我想使用一个 XmlDataProvider 与一些静态数据,然后我可以在一个 CollectionViewSource 元素,如下面的例子。http:/cromwellhaus.com201003grouping-is-crazy-easy-in-wpf。 (归档).

我曾试着这样做。

<CollectionViewSource x:Key="CollectionViewSource1" Source="{Binding Path=ToDoList}">
    <CollectionViewSource.GroupDescriptions>
        <PropertyGroupDescription PropertyName="TodoType"/>
    </CollectionViewSource.GroupDescriptions>
</CollectionViewSource>

但是我得到一个运行时绑定错误:

BindingExpression产生的值对目标属性无效; Value='System.Windows.Data.ListCollectionView' BindingExpression:Path=ToDoList; DataItem='ToDosViewModel'(HashCode=40956219); 目标元素是'CollectionViewSource'(HashCode=51380674); 目标属性是'Source'(类型为'Object')。

wpf grouping itemscontrol itemssource
2个回答
1
投票

但是我收到一个运行时绑定错误,说ItemsSource是无效类型。

你是否正确引用了它?你需要指定它为 Binding.Source:

ItemsSource="{Binding Source={StaticResource CollectionViewSource1}}"

0
投票

我在C#中使用了ListCollectionView而不是XAML中的CollectionViewSource。

© www.soinside.com 2019 - 2024. All rights reserved.