Disable comboxboxitem by binding to data bool

By : WJM
Source: Stackoverflow.com
Question!

I have a ComboBox in a UWP project. I am binding the DataSource to a collection of MyItem class. My class looks like this:

public class MyItem : INotifyPropertyChanged
{
    #region INPC
    public event PropertyChangedEventHandler PropertyChanged;

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

    }



    #endregion
    private string _ItemName;
    public string ItemName
    {
        get { return _ItemName; }
        set
        {
            if (value != _ItemName)
            {
                _ItemName = value;
                Notify("ItemName");
            }
        }
    }

    private bool _ItemEnabled;
    public bool ItemEnabled
    {
        get { return _ItemEnabled; }
        set
        {
            if (value != _ItemEnabled)
            {
                _ItemEnabled = value;
                Notify("ItemEnabled");
            }
        }
    }}

Now I want the ComboBoxItem to be enabled or disabled depending on my ItemEnabled property. I researched and tried adding a binding through the Style tag but the binding does not function in UWP.

<ComboBox.ItemContainerStyle>
      <Style TargetType="ComboBoxItem">
         <Setter Property="IsEnabled" Value="{Binding ItemEnabled}" />
      </Style>
</ComboBox.ItemContainerStyle>

How can I solve this?

EDIT 1: Binding code

<ComboBox ItemsSource="{Binding Path=MyItemsCollection, UpdateSourceTrigger=PropertyChanged}">           
  <ComboBox.ItemTemplate> 
    <DataTemplate>      
      <TextBlock Text="{Binding Path=ItemName}" />  
    </DataTemplate>
   </ComboBox.ItemTemplate> 
</ComboBox>
By : WJM


Answers

Just remove this line (ItemsSource="{Binding Path=MyItemsCollection, UpdateSourceTrigger=PropertyChanged}") in XAML and add this line after InitializeComponent(); in the code behind:

By : S.Akbari


This video can help you solving your question :)
By: admin