将 Wpf HierarchicalDataTemplate ItemsSource 绑定到字典中的 CollectionViewSource?

发布于 2024-08-18 11:39:35 字数 542 浏览 10 评论 0原文

我正在尝试显示一个 Wpf Treeview,其中包含按 CollectionViewSource 排序的项目。

目前,除了在我的资源字典中使用此代码进行排序之外,一切正常:

<HierarchicalDataTemplate DataType="{x:Type books:Container}" ItemsSource="{Binding Path=Items}">
    <nav:ContainerControl />
</HierarchicalDataTemplate>

更改 HierarchicalDataTemplate 以绑定到 CollectionViewSource(反过来从 Items 属性中提取)的语法是什么?

我尝试了Bea Stollnitz 博客上发布的代码的变体,但没有成功。我不知道如何设置 CollectionViewSource 的源。

I'm trying to display a Wpf Treeview with items sorted by a CollectionViewSource.

Currently, everything is working except sorting using this code in my resource dictionary:

<HierarchicalDataTemplate DataType="{x:Type books:Container}" ItemsSource="{Binding Path=Items}">
    <nav:ContainerControl />
</HierarchicalDataTemplate>

What would be the syntax for changing the HierarchicalDataTemplate to bind to a CollectionViewSource that in turn pulls from the Items property?

I've tried variations of the code posted on Bea Stollnitz's blog with no success. I can't figure out how to set the source of the CollectionViewSource.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甚是思念 2024-08-25 11:39:35

好吧,我只是说我讨厌我提出的解决方案,但它确实有效。也许 WPF 大师会为我们俩提供更好的选择。当然,如果您在视图后面使用 ViewModel,则只需在 ViewModel 中使用 CollectionView 包装模型的 Items 属性即可完成操作。

但这里有另一个解决方案。基本上,您的 HierarchicalDataTemplate 可以保持原样,除非您将转换器添加到绑定中。我实现了以下转换器并相应地更改了 XAML。

<HierarchicalDataTemplate DataType="{x:Type books:Container}"
    ItemsSource="{Binding Items, Converter={x:Static local:CollectionViewConverter.Instance}}">
    <nav:ContainerControl />
</HierarchicalDataTemplate>

CollectionViewConverter.cs

public class CollectionViewConverter : IValueConverter
{

    public CollectionViewConverter() {}

    static CollectionViewConverter(){
        Instance = new CollectionViewConverter();
    }

    public static CollectionViewConverter Instance {
        get;
        set;
    }

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var view = new ListCollectionView((System.Collections.IList)value);
        view.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending));
        return view;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        // not really necessary could just throw notsupportedexception
        var view = (CollectionView)value;
        return view.SourceCollection;
    }
}

Well let me just say that I hate my proposed solution, but it does work. Perhaps a WPF guru will enlighten us both with a better alternative. Of course if you were using a ViewModel behind your view, you could simply wrap the Items property of the model with a CollectionView in the ViewModel and be done with it.

But here's another solution. Basically, your HierarchicalDataTemplate can stay as is except you would add a Converter to the Binding. I implemented the following converter and changed the XAML accordingly.

<HierarchicalDataTemplate DataType="{x:Type books:Container}"
    ItemsSource="{Binding Items, Converter={x:Static local:CollectionViewConverter.Instance}}">
    <nav:ContainerControl />
</HierarchicalDataTemplate>

CollectionViewConverter.cs

public class CollectionViewConverter : IValueConverter
{

    public CollectionViewConverter() {}

    static CollectionViewConverter(){
        Instance = new CollectionViewConverter();
    }

    public static CollectionViewConverter Instance {
        get;
        set;
    }

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var view = new ListCollectionView((System.Collections.IList)value);
        view.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending));
        return view;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        // not really necessary could just throw notsupportedexception
        var view = (CollectionView)value;
        return view.SourceCollection;
    }
}
许仙没带伞 2024-08-25 11:39:35

我按照你的建议做了,并用 ListCollectionView 包装了 Items 集合:

private SortDescription _ItemsLcvSortDesc;
    private SortDescription ItemsLcvSortDesc
    {
        get
        {
            if (_ItemsLcvSortDesc == null)
                _ItemsLcvSortDesc = new SortDescription("SortOrder", ListSortDirection.Ascending);
            return _ItemsLcvSortDesc;
        }
    }

    private ListCollectionView _ItemsLcv;
    public ListCollectionView ItemsLcv
    {
        get
        {
            if (_ItemsLcv == null)
                _ItemsLcv = CollectionViewSource.GetDefaultView(Items) as ListCollectionView;
            _ItemsLcv.SortDescriptions.Add(ItemsLcvSortDesc);
            return _ItemsLcv;
        }
    }

我错过了什么吗?

I did as you suggested and wrapped the Items collection with a ListCollectionView:

private SortDescription _ItemsLcvSortDesc;
    private SortDescription ItemsLcvSortDesc
    {
        get
        {
            if (_ItemsLcvSortDesc == null)
                _ItemsLcvSortDesc = new SortDescription("SortOrder", ListSortDirection.Ascending);
            return _ItemsLcvSortDesc;
        }
    }

    private ListCollectionView _ItemsLcv;
    public ListCollectionView ItemsLcv
    {
        get
        {
            if (_ItemsLcv == null)
                _ItemsLcv = CollectionViewSource.GetDefaultView(Items) as ListCollectionView;
            _ItemsLcv.SortDescriptions.Add(ItemsLcvSortDesc);
            return _ItemsLcv;
        }
    }

Did I miss anything?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文