项目更改时通知ObservableCollection

通知Observablecollection某项已更改的某些技术。该链接中的TrulyObservableCollection似乎是我正在寻找的东西。


public class TrulyObservableCollection<T> : ObservableCollection<T>

where T : INotifyPropertyChanged

{

    public TrulyObservableCollection()

    : base()

    {

        CollectionChanged += new NotifyCollectionChangedEventHandler(TrulyObservableCollection_CollectionChanged);

    }


    void TrulyObservableCollection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)

    {

        if (e.NewItems != null)

        {

            foreach (Object item in e.NewItems)

            {

                (item as INotifyPropertyChanged).PropertyChanged += new PropertyChangedEventHandler(item_PropertyChanged);

            }

        }

        if (e.OldItems != null)

        {

            foreach (Object item in e.OldItems)

            {

                (item as INotifyPropertyChanged).PropertyChanged -= new PropertyChangedEventHandler(item_PropertyChanged);

            }

        }

    }


    void item_PropertyChanged(object sender, PropertyChangedEventArgs e)

    {

        NotifyCollectionChangedEventArgs a = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset);

        OnCollectionChanged(a);

    }

}

但是,当我尝试使用它时,我没有收到关于集合的通知。我不确定如何在我的C#代码中正确实现这一点:


XAML:


    <DataGrid AutoGenerateColumns="False" ItemsSource="{Binding MyItemsSource, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">

        <DataGrid.Columns>

            <DataGridCheckBoxColumn Binding="{Binding MyProperty, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

        </DataGrid.Columns>

    </DataGrid>

当我运行程序时,与属性初始化一样,我有3个复选框为false,true,false。但是,当我更改ckeckbox之一的状态时,该程序将通过item_PropertyChanged,但永远不会在MyItemsSource属性代码中进行。


互换的青春
浏览 982回答 3
3回答

慕后森

我通过使用静态动作解决了这种情况public class CatalogoModel&nbsp;{&nbsp; &nbsp; private String _Id;&nbsp; &nbsp; private String _Descripcion;&nbsp; &nbsp; private Boolean _IsChecked;&nbsp; &nbsp; public String Id&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; get { return _Id; }&nbsp; &nbsp; &nbsp; &nbsp; set { _Id = value; }&nbsp; &nbsp; }&nbsp; &nbsp; public String Descripcion&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; get { return _Descripcion; }&nbsp; &nbsp; &nbsp; &nbsp; set { _Descripcion = value; }&nbsp; &nbsp; }&nbsp; &nbsp; public Boolean IsChecked&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; get { return _IsChecked; }&nbsp; &nbsp; &nbsp; &nbsp; set&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;_IsChecked = value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NotifyPropertyChanged("IsChecked");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; OnItemChecked.Invoke();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public static Action OnItemChecked;}&nbsp;public class ReglaViewModel : ViewModelBase{&nbsp; &nbsp; private ObservableCollection<CatalogoModel> _origenes;&nbsp; &nbsp; CatalogoModel.OnItemChecked = () =>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var x = Origenes.Count;&nbsp; //Entra cada vez que cambia algo en _origenes&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };}
打开App,查看更多内容
随时随地看视频慕课网APP