ListBox SourceUpdate 的事件处理程序未在 MVVM 中调用



我想在更新DeviceListItem时运行事件处理程序。但是,即使数据在视图上更新,也不会调用 evnet 处理程序。

XAML

<ListBox x:Name="DeviceListItem" ItemsSource="{Binding DeviceListItems, 
                     UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" 
                     SourceUpdated="OnDeviceListItemsUpdated">

视图

private void OnDeviceListItemsUpdated(object sender, EventArgs e)
{
    // to do
}

视图模型

private ObservableCollection<Device> mDeviceListItems;
public ObservableCollection<Device> DeviceListItems
{
    get { return mDeviceListItems;  }
    set { mDeviceListItems = value; 
          RaisePropertyChangedEvent("DeviceListItems"); }
}

我认为您可以使用CollectionChanged of ObservableCollection

 DeviceListItems.CollectionChanged += itemDisplayList_CollectionChanged;
  void itemDisplayList_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
        }

简单地说,我在没有sourceUpdated事件处理程序的情况下解决了它。

    INotifyPropertyChanged viewModel = (INotifyPropertyChanged)DataContext;
    viewModel.PropertyChanged += OnDeviceListItemsUpdated;
}
private void OnDeviceListItemsUpdated(object sender, PropertyChangedEventArgs e)

最新更新