代码之家  ›  专栏  ›  技术社区  ›  Faisal

XAML中CollectionViewSource SortDescription的绑定属性名

  •  6
  • Faisal  · 技术社区  · 16 年前

    这是我的XAML,它告诉CollectionViewSource排序属性名。

    <CollectionViewSource Source="{Binding Contacts}" x:Key="contactsCollection" Filter="CollectionViewSource_Filter">
                <CollectionViewSource.SortDescriptions>
                    <scm:SortDescription PropertyName="DisplayName" />
                </CollectionViewSource.SortDescriptions>
    </CollectionViewSource>
    

    上面的XAML工作得很好,但我遇到的问题是,我不知道如何为SortDescription属性名提供变量值。我的ViewModel中有一个属性,它告诉我要排序哪个属性,但我无法将此属性绑定到SortDescription的PropertyName字段。

    有什么办法吗?

    1 回复  |  直到 16 年前
        1
  •  7
  •   Wallstreet Programmer    16 年前

    您可以在代码隐藏中设置排序描述。

    XAML:

    <Window.Resources>
    
        <CollectionViewSource Source="{Binding People}" x:Key="_peopleCVS" />
    
    </Window.Resources>
    
    <StackPanel>
        <ListBox
            ItemsSource="{Binding Source={StaticResource _peopleCVS}}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding Path=Name}" Margin="5,0"/>
                        <TextBlock Text="{Binding Path=Age}" />
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        <ComboBox SelectionChanged="ComboBox_SelectionChanged">
            <ComboBoxItem>Age</ComboBoxItem>
            <ComboBoxItem>Name</ComboBoxItem>
        </ComboBox>
    </StackPanel>
    

    代码落后:

    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Controls.Primitives;
    using System.Windows.Data;
    
    namespace CollectionViewSourceDemo
    {
        public partial class Window1 : Window
        {
            public Window1()
            {
                InitializeComponent();
    
                People = new List<Person>();
                People.Add(new Person("Bob", 34));
                People.Add(new Person("Sally", 12));
                People.Add(new Person("Joe", 56));
                People.Add(new Person("Mary", 23));
    
                DataContext = this;
            }
    
            public List<Person> People { get; private set; }
    
            private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                ComboBoxItem comboBoxItem = (sender as Selector).SelectedItem as ComboBoxItem;
                string sortProperty = comboBoxItem.Content as string;
                CollectionViewSource cvs = FindResource("_peopleCVS") as CollectionViewSource;
                cvs.SortDescriptions.Clear();
                cvs.SortDescriptions.Add(new SortDescription(sortProperty, ListSortDirection.Ascending));
            }
        }
    
        public class Person
        {
            public Person(string name, int age)
            {
                Name = name;
                Age = age;
            }
    
            public string Name { get; private set; }
            public int Age { get; private set; }
        }
    }