将CollectionViewSource与HierarchicalDataTemplate一起使用

时间:2014-08-04 21:03:56

标签: wpf collectionviewsource hierarchicaldatatemplate

如何结合HierarchicalDataTemplate的ItemsSource使用CollectionViewSource(提供排序行为)?

因此,鉴于以下代码,我如何对层次结构中每个级别的子项应用排序?

<HierarchicalDataTemplate DataType="{x:Type l:CommandGroup}"
                          ItemsSource="{Binding Children}">
                    <HierarchicalDataTemplate.ItemContainerStyle>
                        <Style TargetType="{x:Type MenuItem}"
                               BasedOn="{StaticResource {x:Type MenuItem}}">
                            <Setter Property="Command"
                                    Value="{Binding Command}" />
                            <Setter Property="CommandParameter"
                                    Value="{Binding}" />
                        </Style>
                    </HierarchicalDataTemplate.ItemContainerStyle>
                    <TextBlock Text="{Binding Name}" />
                </HierarchicalDataTemplate>

我认为我可能已经能够执行以下操作,但它会导致后代出现以及产生以下绑定错误。

<HierarchicalDataTemplate DataType="{x:Type l:CommandGroup}">
                    <HierarchicalDataTemplate.ItemsSource>
                        <Binding>
                            <Binding.Source>
                                <CollectionViewSource Source="{Binding Children}" />
                            </Binding.Source>
                        </Binding>
                    </HierarchicalDataTemplate.ItemsSource>
                    <HierarchicalDataTemplate.ItemContainerStyle>
                        <Style TargetType="{x:Type MenuItem}"
                               BasedOn="{StaticResource {x:Type MenuItem}}">
                            <Setter Property="Command"
                                    Value="{Binding Command}" />
                            <Setter Property="CommandParameter"
                                    Value="{Binding}" />
                        </Style>
                    </HierarchicalDataTemplate.ItemContainerStyle>
                    <TextBlock Text="{Binding Name}" />
                </HierarchicalDataTemplate>

System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=Children; DataItem=null; target element is 'CollectionViewSource' (HashCode=5114324); target property is 'Source' (type 'Object')

编辑:我最终得到了以下内容 - 希望它可以帮助其他人

 <Window.Resources>
    <l:SortedCollectionViewSource x:Key="SortedCollectionViewSource" Property="Name"/>
</Window.Resources>

<Window.ContextMenu>
    <ContextMenu>
        <MenuItem Header="Add new item..."
                  ItemsSource="{Binding AddNewItem.Children, Converter={StaticResource SortedCollectionViewSource}}">
            <MenuItem.ItemTemplate>
                <HierarchicalDataTemplate DataType="{x:Type l:CommandGroup}"
                                          ItemsSource="{Binding Children, Converter={StaticResource SortedCollectionViewSource}}">
                    <HierarchicalDataTemplate.ItemContainerStyle>
                        <Style TargetType="{x:Type MenuItem}"
                               BasedOn="{StaticResource {x:Type MenuItem}}">
                            <Setter Property="Command"
                                    Value="{Binding Command}" />
                            <Setter Property="CommandParameter"
                                    Value="{Binding}" />
                        </Style>
                    </HierarchicalDataTemplate.ItemContainerStyle>
                    <TextBlock Text="{Binding Name}" />
                </HierarchicalDataTemplate>
            </MenuItem.ItemTemplate>
        </MenuItem>
    </ContextMenu>
</Window.ContextMenu>

public class SortedCollectionViewSource : IValueConverter
{
    public string Property { get; set; }

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var cvs = new CollectionViewSource() { Source = value };
        cvs.SortDescriptions.Add(new SortDescription(Property, ListSortDirection.Ascending));

        return cvs.View;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

1 个答案:

答案 0 :(得分:0)

因错误称CollectionViewSource不是FrameworkElement元素,因此绑定将无法解析属性值。

作为解决方案,您可以在视图模型中拥有相同的只读属性,并将其绑定到HierarchicalDataTemplate.ItemsSource

例如

    public CollectionViewSource MyCollectionView
    {
        get { return new CollectionViewSource() { Source = Children }; }
    }

并将其绑定为

    <HierarchicalDataTemplate DataType="{x:Type l:CommandGroup}"
                              ItemsSource="{Binding MyCollectionView}">

CollectionViewSource是xaml,通常可以绑定到静态/动态资源或静态属性,其他绑定总是有问题,因为CollectionViewSource不传递DataContext

作为尝试,您可以尝试,但不确定是否有效

<HierarchicalDataTemplate x:name="hTemplate" DataType="{x:Type l:CommandGroup}">
    <HierarchicalDataTemplate.ItemsSource>
        <CollectionViewSource Source="{Binding DataContext.Children,ElementName=hTemplate}" />

如上所述抛出异常尝试这个,如果CollectionViewSource能够解析正确的源集合,这可能会起作用

<HierarchicalDataTemplate x:name="hTemplate" DataType="{x:Type l:CommandGroup}">
    <HierarchicalDataTemplate.ItemsSource>
        <Binding>
            <Binding.Source>
                <CollectionViewSource Source="{Binding DataContext.Children,ElementName=hTemplate}" />