当当前 ObservableCollection 中的属性发生更改时,更新不同的 ObservableCollection

发布于 2024-12-04 10:25:11 字数 1019 浏览 0 评论 0原文

我有一个具有以下绑定的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一桥轻雨一伞开 2024-12-11 10:25:12

我认为您需要为该特定类实现 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.

养猫人 2024-12-11 10:25:12

您的类需要实现 INotifyPropertyChanged:

class ClassA : INotifyPropertyChanged
{
    private bool _isEnabled;
    public bool IsEnabled
    {
        get { return _isEnabled; }
        set
        {
            if (value != _isEnabled)
            {
                _isEnabled = value;
                OnPropertyChanged("IsEnabled");
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

然后在 SelectedItems 的 setter 中包括:(

OnPropertyChanged("SelectableItems");

来自 这个答案

Your class needs to implement INotifyPropertyChanged:

class ClassA : INotifyPropertyChanged
{
    private bool _isEnabled;
    public bool IsEnabled
    {
        get { return _isEnabled; }
        set
        {
            if (value != _isEnabled)
            {
                _isEnabled = value;
                OnPropertyChanged("IsEnabled");
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

Then in your setter for SelectedItems include:

OnPropertyChanged("SelectableItems");

(From this answer)

怀念你的温柔 2024-12-11 10:25:12

好的,所以我将 SelectedItems ObservableCollection 中的每个项目订阅到 PropertyChanged 事件,然后(回答问题)使用 LINQ 从 SelectableItems ObservableCollection 中获取 SelectableItem 并设置属性:

    public void SelectedItemPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        SelectedItem item = (SelectedItem)sender;

        var selectableItem = (from SelectableItem in _selectableItems
                           where (SelectableItem.Name == item.Name)
                           select SelectableItem).First();

        _selectableItems[selectableItem.Index].IsSelectable = false;
    }

如果有更好的解决方案 - 请告诉我,谢谢。

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 :

    public void SelectedItemPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        SelectedItem item = (SelectedItem)sender;

        var selectableItem = (from SelectableItem in _selectableItems
                           where (SelectableItem.Name == item.Name)
                           select SelectableItem).First();

        _selectableItems[selectableItem.Index].IsSelectable = false;
    }

If there is a better solution - let me know, Thanks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文