我尝试通过CollectionViewSource和转换器类(CollectionViewFactoryConverter)对HierachicalDataTemplate进行排序,这应该是能够对树视图的所有级别进行排序的完美解决方案。我使用DevExpress的DXT列表,但我认为这不是我问题的根源。
我的问题:转换器永远不会被触发。我可以在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;
}
}
答案 0 :(得分:0)
尝试在TreeListControl中的ItemsSource的绑定中调用转换器:
ItemsSource="{Binding Source={StaticResource cvsRoot}, Converter={StaticResource collectionViewFactoryConverter}}"