3

I have this model:

public class Option : BindableBase
{
    private bool _enabled;
    public bool Enabled
    {
        get
        {
            return _enabled;
        }
        set
        {
            SetProperty(ref _enabled, value);
        }
    }

    private string _value;
    public string Value
    {
        get
        {
            return _value;
        }
        set
        {
            SetProperty(ref _value, value);
        }
    }
}

In my viewmodel, I got a list Options of type ObservableCollection<Option>.

I use this XAML piece of code:

<ComboBox Width="200"
          ItemsSource="{Binding Options}">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Value}"/>
                <TextBlock Text="{Binding Enabled}"/>
            </StackPanel>
        </DataTemplate>
     </ComboBox.ItemTemplate>
     <ComboBox.ItemContainerStyle>
         <Style TargetType="ComboBoxItem">
             <Setter Property="IsEnabled" Value="{Binding Enabled}"/>
         </Style>
     </ComboBox.ItemContainerStyle>
 </ComboBox>

I add some Option in my list, some of them have the Enabled property set to true while others do not.

However, the ones having the property to false are still enabled in the ComboBox and I can select them (while I should not!). When using this line of code:

 <Setter Property="IsEnabled" Value="false"/>

The options are effectively disabled (I can't select any of them) but I'd like to use some binding. Can someone explain what I'm doing wrong here?

4

2 回答 2

1

您在 ComboBoxItem 的内容上设置 IsEnabled,而您需要在 ComboBoxItem 本身上设置它。您需要在 ItemContainerStyle 中设置绑定。我不确定样式设置器是否支持绑定。如果不是,那么您可能需要使用解决方法来设置绑定。

于 2013-09-13T15:58:28.017 回答
-1

也许您需要改用路径。你可以试试这个:

<Setter Property="IsEnabled" Value="{Binding Path=Enabled}"/>
于 2013-09-13T15:12:32.823 回答