尝试通过 CollectionViewSource 和 Converter 对 HierachicalDataTemplate 进行排序时,永远不会调用 Converter

发布于 2024-12-29 14:13:14 字数 5363 浏览 0 评论 0原文

我尝试通过 CollectionViewSource 和转换器类 (CollectionViewFactoryConverter) 对 HierachicalDataTemplate 进行排序,这应该是能够对树视图的所有级别进行排序的完美解决方案。我使用 DevExpress 的 DXTreelist,但我认为这不是问题的根源。

我的问题:转换器永远不会被触发。我可以在 Convert 或 ConvertBack 方法中放置一个断点,但我永远不会在那里结束。我不明白为什么没有反应。 - 有人能帮忙吗?

WPF 代码:

<Window.Resources>

    <view:CollectionViewFactoryConverter x:Key="collectionViewFactoryConverter" />

    <local:MyTemplateSelector x:Key="templateSelector" />
    <local:ContentToTreeListNodeConverter x:Key="contentToTreeListNodeConverter"/>

    <CollectionViewSource Source="{Binding Path=Data, Mode=TwoWay}" x:Key="cvsRoot">
        <CollectionViewSource.SortDescriptions>
            <scm:SortDescription PropertyName="Name" />
        </CollectionViewSource.SortDescriptions>
    </CollectionViewSource>

    <DataTemplate x:Key="StandardTemplate">
        <TextBlock Text="Template Not Found!" />
    </DataTemplate>

    <HierarchicalDataTemplate x:Key="RootTemplate"
        ItemsSource="{Binding RefA, Converter={StaticResource collectionViewFactoryConverter}, ConverterParameter=Row.Name}">
        <TextBlock Text="{Binding Row.Name}" />
    </HierarchicalDataTemplate>

    <!--Code below is working, but results are unsorted-->
    <!--<HierarchicalDataTemplate x:Key="RootTemplate" ItemsSource="{Binding RefA}">                                  
        <TextBlock Text="{Binding Row.Name}" />
    </HierarchicalDataTemplate>-->

    <HierarchicalDataTemplate x:Key="ModelATemplate" ItemsSource="{Binding RefB}">
        <TextBlock Text="{Binding Row.Name}" />
    </HierarchicalDataTemplate>

    <HierarchicalDataTemplate x:Key="ModelBTemplate">
        <TextBlock Text="{Binding Row.Name}" />
    </HierarchicalDataTemplate>        

</Window.Resources>

TreelistControl 的代码:

    <dxgcore:TreeListControl Name="treeListControl"
        DesignTimeShowSampleData="False"
        ItemsSource="{Binding Source={StaticResource cvsRoot}}"
        Grid.ColumnSpan="2" Margin="123,0,0,0" ItemsSourceChanged="treeListControl_ItemsSourceChanged">

        <dxg:TreeListControl.View>
            <dxg:TreeListView Name="tlvList" 
            IsColumnMenuEnabled="False"
            AllowPerPixelScrolling="True"
            AutoWidth="True"
            ShowHorizontalLines="False" 
            ShowVerticalLines="False"
            ShowIndicator="False" 
            ShowFocusedRectangle="False" 
            NavigationStyle="Row"
            TreeLineStyle="Solid"                                  
            FixedLineWidth="1" 
            FilterEditorShowOperandTypeIcon="True" 
            DataRowTemplateSelector="{StaticResource templateSelector}"
            TreeDerivationMode="HierarchicalDataTemplate"
            FocusedNode="{Binding HierarchicalFilterSelection, Mode=OneWayToSource}"
        >
            </dxg:TreeListView>
        </dxg:TreeListControl.View>
    </dxgcore:TreeListControl>

转换器类:

[ValueConversion(typeof(System.Collections.IList), typeof(System.Collections.IEnumerable))]

public class CollectionViewFactoryConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        System.Collections.IList collection = (System.Collections.IList)value as System.Collections.IList;
        ListCollectionView view = new ListCollectionView(collection);
        SortDescription sort = new SortDescription(parameter.ToString(), ListSortDirection.Ascending);
        view.SortDescriptions.Add(sort);
        return view;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return null;
    }
}

模板选择器看起来像这样 BTW:

public class MyTemplateSelector : DataTemplateSelector
{
    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        DataTemplate template = null;
        if (item != null)
        {
            FrameworkElement element = container as FrameworkElement;
            if (element != null)
            {

                string templateName = "StandardTemplate";
                DevExpress.Xpf.Grid.TreeList.TreeListRowData itemAsTreelistRowData = null;

                if (item is DevExpress.Xpf.Grid.TreeList.TreeListRowData)
                {
                    itemAsTreelistRowData = item as DevExpress.Xpf.Grid.TreeList.TreeListRowData;
                }

                if (itemAsTreelistRowData.Row is Root)
                {
                    templateName = "RootTemplate";
                }
                else
                    if (itemAsTreelistRowData.Row is ModelA)
                    {
                        templateName = "ModelATemplate";
                    }
                    else

                        if (itemAsTreelistRowData.Row is ModelB)
                        {
                            templateName = "ModelBTemplate";
                        }

                template = element.FindResource(templateName) as DataTemplate;
            }
        }
        return template;
    }

}

I try to sort a HierachicalDataTemplate via CollectionViewSource and a Converter Class (CollectionViewFactoryConverter), which should be the perfect solution to be able to sort all levels of the treeview. I use a DXTreelist from DevExpress, but I assume this isn not the source of my problem.

My problem: The Converter is never triggered. I can put a breakpoint into the Convert or the ConvertBack method, but I never end up there. I can't figure it out why there is no reaction. - Can anyone help?

WPF Code:

<Window.Resources>

    <view:CollectionViewFactoryConverter x:Key="collectionViewFactoryConverter" />

    <local:MyTemplateSelector x:Key="templateSelector" />
    <local:ContentToTreeListNodeConverter x:Key="contentToTreeListNodeConverter"/>

    <CollectionViewSource Source="{Binding Path=Data, Mode=TwoWay}" x:Key="cvsRoot">
        <CollectionViewSource.SortDescriptions>
            <scm:SortDescription PropertyName="Name" />
        </CollectionViewSource.SortDescriptions>
    </CollectionViewSource>

    <DataTemplate x:Key="StandardTemplate">
        <TextBlock Text="Template Not Found!" />
    </DataTemplate>

    <HierarchicalDataTemplate x:Key="RootTemplate"
        ItemsSource="{Binding RefA, Converter={StaticResource collectionViewFactoryConverter}, ConverterParameter=Row.Name}">
        <TextBlock Text="{Binding Row.Name}" />
    </HierarchicalDataTemplate>

    <!--Code below is working, but results are unsorted-->
    <!--<HierarchicalDataTemplate x:Key="RootTemplate" ItemsSource="{Binding RefA}">                                  
        <TextBlock Text="{Binding Row.Name}" />
    </HierarchicalDataTemplate>-->

    <HierarchicalDataTemplate x:Key="ModelATemplate" ItemsSource="{Binding RefB}">
        <TextBlock Text="{Binding Row.Name}" />
    </HierarchicalDataTemplate>

    <HierarchicalDataTemplate x:Key="ModelBTemplate">
        <TextBlock Text="{Binding Row.Name}" />
    </HierarchicalDataTemplate>        

</Window.Resources>

Code of the TreelistControl:

    <dxgcore:TreeListControl Name="treeListControl"
        DesignTimeShowSampleData="False"
        ItemsSource="{Binding Source={StaticResource cvsRoot}}"
        Grid.ColumnSpan="2" Margin="123,0,0,0" ItemsSourceChanged="treeListControl_ItemsSourceChanged">

        <dxg:TreeListControl.View>
            <dxg:TreeListView Name="tlvList" 
            IsColumnMenuEnabled="False"
            AllowPerPixelScrolling="True"
            AutoWidth="True"
            ShowHorizontalLines="False" 
            ShowVerticalLines="False"
            ShowIndicator="False" 
            ShowFocusedRectangle="False" 
            NavigationStyle="Row"
            TreeLineStyle="Solid"                                  
            FixedLineWidth="1" 
            FilterEditorShowOperandTypeIcon="True" 
            DataRowTemplateSelector="{StaticResource templateSelector}"
            TreeDerivationMode="HierarchicalDataTemplate"
            FocusedNode="{Binding HierarchicalFilterSelection, Mode=OneWayToSource}"
        >
            </dxg:TreeListView>
        </dxg:TreeListControl.View>
    </dxgcore:TreeListControl>

Converter Class:

[ValueConversion(typeof(System.Collections.IList), typeof(System.Collections.IEnumerable))]

public class CollectionViewFactoryConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        System.Collections.IList collection = (System.Collections.IList)value as System.Collections.IList;
        ListCollectionView view = new ListCollectionView(collection);
        SortDescription sort = new SortDescription(parameter.ToString(), ListSortDirection.Ascending);
        view.SortDescriptions.Add(sort);
        return view;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return null;
    }
}

The template selector looks like this BTW:

public class MyTemplateSelector : DataTemplateSelector
{
    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        DataTemplate template = null;
        if (item != null)
        {
            FrameworkElement element = container as FrameworkElement;
            if (element != null)
            {

                string templateName = "StandardTemplate";
                DevExpress.Xpf.Grid.TreeList.TreeListRowData itemAsTreelistRowData = null;

                if (item is DevExpress.Xpf.Grid.TreeList.TreeListRowData)
                {
                    itemAsTreelistRowData = item as DevExpress.Xpf.Grid.TreeList.TreeListRowData;
                }

                if (itemAsTreelistRowData.Row is Root)
                {
                    templateName = "RootTemplate";
                }
                else
                    if (itemAsTreelistRowData.Row is ModelA)
                    {
                        templateName = "ModelATemplate";
                    }
                    else

                        if (itemAsTreelistRowData.Row is ModelB)
                        {
                            templateName = "ModelBTemplate";
                        }

                template = element.FindResource(templateName) as DataTemplate;
            }
        }
        return template;
    }

}

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

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

发布评论

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

评论(1

〃温暖了心ぐ 2025-01-05 14:13:14

尝试在 TreeListControl 中的 ItemsSource 绑定中直接调用转换器:

  ItemsSource="{Binding Source={StaticResource cvsRoot}, Converter={StaticResource collectionViewFactoryConverter}}"

Try calling the converter right in the binding for the ItemsSource in your TreeListControl:

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