WPF排序ItemsControl绑定

时间:2017-12-07 16:30:57

标签: c# wpf sorting xaml

我目前正在尝试按名称排序ItemsControl。目前我已打印出列表,但似乎无法在Views侧订购。我可以在Control或model侧订购它,但希望它能从视图中工作。

我的ItemsControl绑定了AllJobTypes(类JobTypes的列表)。 JobTypes有一个名为Name的属性,我想在视图中对其进行排序。

我在XAML中有一些调试代码,用于打印每个对象的计数。前2个打印出来'失败'最后一个正常工作。如何在视图方面订购AllJobTypes

<UserControl.Resources>
    <converters:JobTypeCreditUnionCountConverter x:Key="JobTypeCreditUnionCountConverter" />
    <CollectionViewSource x:Key="cvs" Source="{Binding RelativeSource={RelativeSource 
                        AncestorType=UserControl}, Path=AllJobTypes}">
        <CollectionViewSource.SortDescriptions>
            <scm:SortDescription PropertyName="Name"/>
        </CollectionViewSource.SortDescriptions>
    </CollectionViewSource>
</UserControl.Resources>
<StackPanel>

    <Label Foreground="SteelBlue" FontSize="20" FontWeight="Bold">Job Types</Label>
    <Label Content="{Binding AllJobTypes.Count, FallbackValue='fail'}" />  //Fail
    <Label Content="{Binding cvs.Count, FallbackValue='fail'}" />  //Fail
    <Label Content="{Binding RelativeSource={RelativeSource 
                        AncestorType=UserControl}, Path=AllJobTypes}" /> //(Collection
    <ItemsControl ItemsSource="{Binding cvs}" >
        <ItemsControl.ItemTemplate>
        <DataTemplate>
                <Grid Margin="5">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="3*"/>
                        <ColumnDefinition Width="6*"/>
                        <ColumnDefinition Width="2*"/>
                        <ColumnDefinition Width="1*"/>
                    </Grid.ColumnDefinitions>
                    <TextBlock Grid.Column="0" Text="" />
                    <TextBox Grid.Column="1" Text="{Binding Name}" IsEnabled="False" />

1 个答案:

答案 0 :(得分:1)

尝试:

<ItemsControl ItemsSource="{Binding Source={StaticResource cvs}}" >
相关问题