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?

Max
  • 3,453
  • 3
  • 32
  • 50
  • possible duplicate of [How do I do bindings in ItemContainerStyle in WinRT?](http://stackoverflow.com/questions/11857505/how-do-i-do-bindings-in-itemcontainerstyle-in-winrt) – WiredPrairie Sep 13 '13 at 15:59
  • It may lead to same answers, but it's a different question, so it helps to have more questions to make it easier to find the answer. – Filip Skakun Sep 16 '13 at 15:06

2 Answers2

1

You are setting IsEnabled on the content of your ComboBoxItem while you would need to set it on the ComboBoxItem itself. You would need to set the binding in the ItemContainerStyle. I'm not sure if bindings are supported in style setters though. If they are not, then you might need to use a workaround to set up the binding.

Filip Skakun
  • 31,624
  • 6
  • 74
  • 100
-1

Maybe you need to use path instead. You can try this :

<Setter Property="IsEnabled" Value="{Binding Path=Enabled}"/>
Burk
  • 2,969
  • 1
  • 23
  • 24