通过绑定到数据布尔禁用 comboxboxitem

Disable comboxboxitem by binding to data bool

我在 UWP 项目中有一个 ComboBox。我将 DataSource 绑定到 MyItem class 的集合。我的 class 看起来像这样:

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");
            }
        }
    }}

现在我希望根据我的 ItemEnabled 属性 启用或禁用 [​​=16=]。我研究并尝试通过 Style 标签添加绑定,但绑定在 UWP 中不起作用。

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

我该如何解决这个问题?

编辑 1:绑定代码

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

只需删除XAML中的这一行(ItemsSource="{Binding Path=MyItemsCollection, UpdateSourceTrigger=PropertyChanged}"),并在后面的代码InitializeComponent();之后添加这一行:

<ComboBox Name="cmb1">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Path=ItemName}" />
        </DataTemplate>
    </ComboBox.ItemTemplate>
    <ComboBox.ItemContainerStyle>
        <Style TargetType="ComboBoxItem">
           <Setter Property="IsEnabled" Value="{Binding ItemEnabled}" />
        </Style>
    </ComboBox.ItemContainerStyle>
</ComboBox>

xaml.cs中:

public Window1()
{
    InitializeComponent();
    cmb1.ItemsSource = MyItemsCollection;
}

编辑:另一种方式是这样的:

public Window1()
{
   InitializeComponent();
   this.DataContext = MyItemsCollection;
}

并且在 xaml 中:

<ComboBox Name="cmb1" ItemsSource="{Binding}">
  ....