尝试通过 CollectionViewSource 和 Converter 对 HierachicalDataTemplate 进行排序时,永远不会调用 Converter
我尝试通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试在 TreeListControl 中的 ItemsSource 绑定中直接调用转换器:
Try calling the converter right in the binding for the ItemsSource in your TreeListControl: