【问题标题】:WPF Sorting ItemsControl Binding [duplicate]WPF排序项目控件绑定[重复]
【发布时间】:2017-12-08 00:34:51
【问题描述】:

我目前正在尝试按名称对ItemsControl 进行排序。目前我有打印出来的列表,但似乎无法在视图端订购它。我可以在控件或模型端订购它,但希望它在视图中工作。

我的 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" />

【问题讨论】:

  • 3 小时前,您接受了ItemsSource="{Binding Source={StaticResource cvs}}" 的答复。为什么你现在不一样了?

标签: c# wpf sorting xaml


【解决方案1】:

试试:

<ItemsControl ItemsSource="{Binding Source={StaticResource cvs}}" >

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-01
    • 1970-01-01
    • 2013-04-13
    • 1970-01-01
    • 1970-01-01
    • 2014-08-25
    • 1970-01-01
    相关资源
    最近更新 更多