当当前 ObservableCollection 中的属性发生更改时,更新不同的 ObservableCollection
我有一个具有以下绑定的 ItemsControl:
ItemsControl ItemsSource="{Binding SelectedItems}
集合中有 10 个 SelectedItems,每个都有一个具有此绑定的组合框:
ComboBox ItemsSource="{Binding SelectableItems}
当选择 SelectableItem 时,SelectedItems ObservableCollection 会更新。然后我希望禁用 SelectableItem,这样就无法在任何其他组合框中选择它。
SelectableItem 的数量不等于 SelectedItem 的数量。
我在 ComboBoxItem 上有以下样式:
<Style TargetType="{x:Type ComboBoxItem}">
<Setter Property="IsEnabled" Value="{Binding IsSelectable}"/>
</Style>
这在启动时工作正常 - 但问题是因为我正在更新 SelectedItems ObservableCollection,我没有更新 SelectableItems 集合..
有什么(LINQ?)我可以放入SelectedItem.PropertyChanged 事件来更新 SelectableItems 集合中的相关项目 - 例如:
public void SelectedItemObservableCollectionPropertyChanged(object sender, PropertyChangedEventArgs e)
{
OnPropertyChanged("SelectableItem.IsSelectable");
}
如果没有,最好的解决方法是什么?
I have an ItemsControl with the following binding :
ItemsControl ItemsSource="{Binding SelectedItems}
There are 10 SelectedItems in the collection, each having a Combo Box with this binding :
ComboBox ItemsSource="{Binding SelectableItems}
When a SelectableItem is selected the SelectedItems ObservableCollection is updated. I would then like the SelectableItem to be disabled, so it cannot be selected in any of the other combo boxes.
The number of SelectableItems is not equal to the number of SelectedItems.
I have the following style on a ComboBoxItem :
<Style TargetType="{x:Type ComboBoxItem}">
<Setter Property="IsEnabled" Value="{Binding IsSelectable}"/>
</Style>
This works fine on Start up - but the problem is because I'm updating the SelectedItems ObservableCollection, I'm not updating the SelectableItems collection..
Is there anything (LINQ?) I can put in SelectedItem.PropertyChanged event to update the relevant item in the SelectableItems collection - so something like :
public void SelectedItemObservableCollectionPropertyChanged(object sender, PropertyChangedEventArgs e)
{
OnPropertyChanged("SelectableItem.IsSelectable");
}
If not, what is the best workaround?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您需要为该特定类实现 INotifyPropertyChanged 并且需要通知属性更改。如果您需要更多说明,请告诉我。
I think you need to implement INotifyPropertyChanged for that particular class and you need to notify on property changed. Let me know if you need more clarification.
您的类需要实现 INotifyPropertyChanged:
然后在 SelectedItems 的 setter 中包括:(
来自 这个答案)
Your class needs to implement INotifyPropertyChanged:
Then in your setter for SelectedItems include:
(From this answer)
好的,所以我将 SelectedItems ObservableCollection 中的每个项目订阅到 PropertyChanged 事件,然后(回答问题)使用 LINQ 从 SelectableItems ObservableCollection 中获取 SelectableItem 并设置属性:
如果有更好的解决方案 - 请告诉我,谢谢。
Ok, so I subscribed each item in the SelectedItems ObservableCollection to a PropertyChanged event, then (answers the question) get the SelectableItem from the SelectableItems ObservableCollection using LINQ and set the property :
If there is a better solution - let me know, Thanks.