当前位置: 首页 > 编程语言 > C#

ObservableCollectionPropertiesChangedtoItemsinCollection分享

时间:2023-04-11 03:35:34 C#

ObservableCollectionPropertiesChangedtoItemsinCollection我有一个ObservableCollection。我已将它绑定到ListBox控件,我已将SortDescriptions放入ListBox上的Items集合中,以便按照我的方式对列表进行排序。我想在子元素上更改任何属性时随时使用该列表。我的所有子元素都实现了INotifyPropertyChanged。蛮力:将处理程序附加到每个子项的每个PropertyChanged事件从CollectionViewSource调用刷新获取ListCollectionView。编辑:1,2的代码将存在于您的代码隐藏中。对于#1,你可以这样做:;}休息;....**在这里处理其他情况**....}}对于#2,在CollectionChanged处理程序中,您可以执行以下操作:.GetDefaultView(theListBox.ItemsSource));lcv.Refresh();EDIT2:但是,在这种情况下,我强烈建议您还检查ListCollectionView.NeedsRefresh并且仅当您设置刷新时。如果您的属性已更改且不影响排序,则没有理由重新排序。这很好用。只要集合发生变化,它就会重新排序集合。可能以更有效的方式实施,但这就是它的要点。publicpartialclassTestWindow:Window{ObservableCollectionoc;publicTestWindow(){InitializeComponent();//填写测试OCoc=newObservableCollection();foreach(charcin"abcdefghieddjko"){oc.Add(newTestClass(c.ToString(),c.ToString(),c.GetHashCode()));}lstbox.ItemsSource=oc;//设置排序(这就是你做的......不起作用)lstbox.Items.SortDescriptions.Add(newSortDescription("A",ListSortDirection.Ascending));//这就是我们要做的事情oc.CollectionChanged+=newSystem.Collections.Specialized.NotifyCollectionChangedEventHandler(oc_Sort);}voidoc_Sort(objectsender,System.Collections.Specialized.NotifyCollectionChangedEventArgse){//这对oc进行排序并返回IEnumerablevaritems=oc.OrderBy((x)=>(xC));//RestconverstIEnumerable回到OC并分配给它ObservableCollectiontemp=newObservableCollection();foreach(variteminitems){temp.Add(项目米);}oc=温度;}privatevoidButton_Click(objectsender,RoutedEventArgse){stringa="grrrr";字符串b="ddddd";整数c=383857;oc.Add(newTestClass(a,b,c));}}publicclassTestClass:INotifyPropertyChanged{privatestringa;私有字符串b;私人诠释c;publicTestClass(stringf,stringg,inti){a=f;b=g;c=我;}公共字符串A{得到{返回一个;}设置{a=值;OnPropertyChanged("A");}}publicstringB{get{返回b;}设置{b=值;OnPropertyChanged("B");}}publicintC{get{返回c;}设置{c=值;OnPropertyChanged("C");}}#regiononpropertychanged公共事件PropertyChangedEventHandlerPropertyChanged;protectedvoidOnPropertyChanged(stringpropertyName){if(this.PropertyChanged!=null){PropertyChanged(this,newPropertyChangedEventArgs(propertyName));}}#endregion}publicpartialclassTestWindow:Window{ObservableCollectionoc;公共TestWindow(){初始化alizeComponent();//填写测试OCoc=newObservableCollection();foreach(charcin"abcdefghieddjko"){oc.Add(newTestClass(c.ToString(),c.ToString(),c.GetHashCode()));}lstbox.ItemsSource=oc;//设置排序(这就是你做的......不起作用)lstbox.Items.SortDescriptions.Add(newSortDescription("A",ListSortDirection.Ascending));//这就是我们要做的事情oc.CollectionChanged+=newSystem.Collections.Specialized.NotifyCollectionChangedEventHandler(oc_Sort);}voidoc_Sort(objectsender,System.Collections.Specialized.NotifyCollectionChangedEventArgse){//这对oc进行排序并返回IEnumerablevaritems=oc.OrderBy((x)=>(xC));//RestconverstIEnumerable回到OC并分配给它ObservableCollectiontemp=newObservableCollection();foreach(variteminitems){temp.Add(item);}oc=温度;}privatevoidButton_Click(objectsender,RoutedEventArgse){stringa="grrrr";stringb="ddddd";intc=383857;oc.Add(newTestClass(a,b,c));}}publicclassTestClass:INotifyPropertyChanged{privatestringa;privatestringb;privateintc;publicTestClass(stringf,stringg,inti){a=f;b=g;c=i;}publicstringA{get{returna;}set{a=value;OnPropertyChanged("A");}}publicstringB{get{returnb;}set{b=value;OnPropertyChanged("B");}}publicintC{get{returnc;}set{c=value;OnPropertyChanged("C");}#regiononpropertychangedpubliceventPropertyChangedEventHandlerPropertyChanged;protectedvoidOnPropertyChanged(stringpropertyName){if(this.PropertyChanged!=null){PropertyChanged(this,newPropertyChangedEventArgs(propertyName));:可观察的集合属性已更改为所有共享的内容收藏中的条目,如果对你有用,需要进一步了解C#学习教程,希望大家多多关注——本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如有转载请注明出处: