WPF MVVM ListView动态绑定数据

北城以北 提交于 2019-12-01 10:41:19

界面

<Window x:Class="WpfApplication8.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ListView ItemsSource="{Binding Path=PersonCollectionView}">
            <ListView.View>
                <GridView>
                    <GridViewColumn DisplayMemberBinding="{Binding Path=Name}">
                        <GridViewColumnHeader Command="{Binding Path=SortCommand}" 
                                              CommandParameter="Name">姓名</GridViewColumnHeader>
                    </GridViewColumn>
                    <GridViewColumn DisplayMemberBinding="{Binding Path=Age}">
                        <GridViewColumnHeader Command="{Binding Path=SortCommand}" 
                                              CommandParameter="Age">年龄</GridViewColumnHeader>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>
</Window>

.cs

 public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = new MainWindowViewModel();
        }
    }

 

新建一个 Person类

 public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }

MainWindowViewModel

 public class MainWindowViewModel
    {
        public MainWindowViewModel()
        {
            personCollection.Add(new Person() { Name = "刘二", Age = 17 });
            personCollection.Add(new Person() { Name = "张三", Age = 18 });
            personCollection.Add(new Person() { Name = "李四", Age = 17 });
            personCollection.Add(new Person() { Name = "王五", Age = 19 });
            personCollection.Add(new Person() { Name = "周六", Age = 18 });
            personCollection.Add(new Person() { Name = "胡二", Age = 16 });
            personCollection.Add(new Person() { Name = "黄七", Age = 14 });
            personCollection.Add(new Person() { Name = "朱八", Age = 15 });
            personCollection.Add(new Person() { Name = "周四", Age = 13 });
            this.SortCommand = new DelegateCommand<string>(this.Sort);
        }

        private ObservableCollection<Person> personCollection = new ObservableCollection<Person>();

        private ICollectionView personCollectionView;
        public ICollectionView PersonCollectionView
        {
            get
            {
                if (personCollectionView == null)
                {
                    personCollectionView = CollectionViewSource.GetDefaultView(personCollection);
                }
                return personCollectionView;
            }
        }

        public DelegateCommand<string> SortCommand { get; private set; }

        private void Sort(string obj)
        {
            if (PersonCollectionView.SortDescriptions.Count > 0 &&
                PersonCollectionView.SortDescriptions[0].PropertyName == obj &&
                PersonCollectionView.SortDescriptions[0].Direction == ListSortDirection.Ascending)
            {
                PersonCollectionView.SortDescriptions.Clear();
                PersonCollectionView.SortDescriptions.Add(new SortDescription(obj, ListSortDirection.Descending));
            }
            else
            {
                PersonCollectionView.SortDescriptions.Clear();
                PersonCollectionView.SortDescriptions.Add(new SortDescription(obj, ListSortDirection.Ascending));
            }
        }
    }

效果

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!