WPF MVVM ListView动态绑定数据

Posted cityofthousandfires

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WPF MVVM ListView动态绑定数据相关的知识,希望对你有一定的参考价值。

技术图片技术图片?

界面

技术图片技术图片?

<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));
            }
        }
    }
技术图片

效果

技术图片技术图片?

以上是关于WPF MVVM ListView动态绑定数据的主要内容,如果未能解决你的问题,请参考以下文章

WPF中为ListView动态绑定数据(可参考)

C#WPF,ListView和GridView,动态列以及数据绑定问题

WPF MVVM模式下实现ListView下拉显示更多内容

如何将listView的selectedItem值传递到wpf MVVM中的另一页

是否可以在 mvvm 模式中获取 wpf 数据网格上的动态列?

WPF MVVM:如何将 GridViewColumn 绑定到 ViewModel-Collection?