p>VM like this:public class TestVM : INotifyPropertyChanged{ private int _index; public int Index { get { return _index; } set { _index = value; RaisePropertyChanged("Index"); } } public event PropertyChangedEventHandler PropertyChanged; private void RaisePropertyChanged(String propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } private User _item; public User Item { get { return _item; } set { _item = value; RaisePropertyChanged("Item"); } } private string _selectedGroup; public string SelectedGroup { get { return _selectedGroup; } set { _selectedGroup = value; RaisePropertyChanged("SelectedGroup"); } } ObservableCollection<User> _collection; public ObservableCollection<User> Collection { get { return _collection; } private set { _collection = value; RaisePropertyChanged("Collection"); } } ListCollectionView _groupView; public ListCollectionView GroupView { get { return _groupView; } private set { _groupView = value; RaisePropertyChanged("GroupView"); } } public TestVM() { Collection = new ObservableCollection<User>(); Collection.Add(new User() { Name = "John Doe1", Age = 10, group = "Group 1" }); Collection.Add(new User() { Name = "Jane Doe2", Age = 20, group = "Group 1" }); Collection.Add(new User() { Name = "Sammy Doe", Age = 30, group = "Group 2" }); Collection.Add(new User() { Name = "Sammy Doe1", Age = 40, group = "Group 2" }); Collection.Add(new User() { Name = "Sammy Doe2", Age = 50, group = "Group 2" }); Collection.Add(new User() { Name = "Sammy Doe3", Age = 60, group = "Group 3" }); Collection.Add(new User() { Name = "Sammy Doe4", Age = 70, group = "Group 3" }); Collection.Add(new User() { Name = "Sammy Doe5", Age = 80, group = "Group 3" }); Collection.Add(new User() { Name = "Sammy Doe6", Age = 90, group = "Group 4" }); Collection.Add(new User() { Name = "Sammy Doe7", Age = 10, group = "Group 4" }); GroupView = new ListCollectionView(Collection); GroupView.GroupDescriptions.Add(new PropertyGroupDescription("group")); }}public class User{ public string Name { set; get; } public int Age { set; get; } public string group { get; set; }}推荐答案我将绑定模式更改为 OneWay ,就可以了。I change Binding Mode to OneWay, then it's work. 这篇关于wpf为什么会得到“ NullReferenceException”绑定SelectedItem时[编辑]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-20 22:38