0

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?

Joe.Net
  • 1,175
  • 5
  • 19
  • 36

3 Answers3

1

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.

Mark Hall
  • 53,938
  • 9
  • 94
  • 111
prawin
  • 413
  • 7
  • 16
  • Thanks PraWiN, Yes, I am implementing INotifyPropertyChanged. – Joe.Net Sep 13 '11 at 09:28
  • Could you please provide some more code so that i can understand your problem. – prawin Sep 13 '11 at 09:33
  • Thanks PraWiN, further clarification : my question is how can I update an Item in Observable Collection 2, when I change a property in Observable Collection 1? – Joe.Net Sep 13 '11 at 09:53
  • Joe, You just need to fire on property changed in setter of any property. public bool MyProperty { get { return myProperty; } set { myProperty= value; OnPropertyChanged("MyProperty"); } } – prawin Sep 13 '11 at 10:12
  • Thanks PraWiN - but aren't you just raising the OnPropertyChanged event for MyProperty in Observable Collection 1 here? What if I want to change ObservableCollection2.AnotherProperty, when MyProperty changes? – Joe.Net Sep 13 '11 at 10:55
  • then you can call OnPropertyChanged("AnotherProperty") in above setter. Hey, it would be better if you post your code over here. – prawin Sep 13 '11 at 11:04
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/3400/discussion-between-prawin-and-joe-net) – prawin Sep 13 '11 at 11:05
0

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)

Community
  • 1
  • 1
Skrealin
  • 1,114
  • 6
  • 16
  • 32
  • Thanks Skrealin, in this case how could I update the ClassB ObservableCollection (SelectableItems) from the ClassA ObservableCollection (SelectedItems)? – Joe.Net Sep 13 '11 at 09:35
  • You can pass ClassB a reference to ClassA when you create it, then call a function that contains the OnPropertyChanged() – Skrealin Sep 13 '11 at 15:49
0

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.

Joe.Net
  • 1,175
  • 5
  • 19
  • 36