如何在TreeView中对DataTemplate进行排序?

时间:2014-05-07 11:37:37

标签: c# wpf xaml treeview collectionviewsource

我有一个功能列表,每个功能都有一个字段列表。 我可以在TreeView控件的帮助下相应地显示它们。

-Feature1
 -FieldMemberA
 -FieldMemberB
-Feature2
  -FieldMemberX

现在我想对功能列表和字段列表进行排序。对功能列表进行排序与​​CollectionViewSource一起使用。但是,我无法对 Fields 列表应用相同的内容。

我复制了工作代码:

<UserControl.Resources>
    <CollectionViewSource Source="{Binding Features}" x:Key="SortedFeatures">
        <CollectionViewSource.SortDescriptions>
            <componentModel:SortDescription Direction="Descending" PropertyName="Prop1"/>
            <componentModel:SortDescription Direction="Ascending" PropertyName="Prop2"/>
        </CollectionViewSource.SortDescriptions>
    </CollectionViewSource>
</UserControl.Resources>
<Grid>     
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Grid>
        <TreeView ItemsSource="{Binding Source={StaticResource SortedFeatures}}">               
            <TreeView.Resources>
                <HierarchicalDataTemplate ItemsSource="{Binding Fields}" DataType="{x:Type model:Feature}">   
                    <RadioButton Name="IsSelectedRadioButton" Content="{Binding Path=FeatureName}" IsChecked="{Binding Path=IsSelected, Mode=TwoWay}" GroupName="selectedFeatureGroup"/>
                </HierarchicalDataTemplate>
                <DataTemplate DataType="{x:Type model:Field}"> 
                    <CheckBox Name="CriteriaCheckBox" Content="{Binding Path=FieldName}" IsChecked="{Binding Path=IsSelected, Mode=TwoWay}"  />
                </DataTemplate >
            </TreeView.Resources>     
        </TreeView>
    </Grid>

我尝试的是:

<CollectionViewSource Source="{Binding Fields}" x:Key="SortedFields">
        <CollectionViewSource.SortDescriptions>
            <componentModel:SortDescription Direction="Descending" PropertyName="Prop1"/>
            <componentModel:SortDescription Direction="Ascending" PropertyName="Prop2"/>
        </CollectionViewSource.SortDescriptions>
    </CollectionViewSource>

<TreeView.Resources>下并绑定到SortedFields而不是Fields

0 个答案:

没有答案
相关问题