在ComboBoxItem上绑定IsEnabled不起作用

时间:2013-09-13 14:53:21

标签: c# xaml windows-8 windows-runtime

我有这个型号:

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

在我的viewmodel中,我得到了Options类型的列表ObservableCollection<Option>

我使用这段XAML代码:

<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>

我在列表中添加了一些Option,其中一些Enabled属性设置为true,而其他属性则没有。{/ p>

但是,具有false属性的那些仍在ComboBox中启用,我可以选择它们(我不应该!)。使用这行代码时:

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

这些选项被有效禁用(我不能选择其中任何一个)但我想使用一些绑定。有人可以解释我在这里做错了吗?

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:-1)

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

<Setter Property="IsEnabled" Value="{Binding Path=Enabled}"/>
相关问题