繁体   English   中英

在ComboBoxItem上绑定IsEnabled不起作用

[英]Binding IsEnabled on ComboBoxItem does not work

我有这个型号:

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

在我的视图模型,我得到了一个列表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 ,其中一些OptionEnabled属性设置为true而其他选项则没有。

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

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

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

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

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

<Setter Property="IsEnabled" Value="{Binding Path=Enabled}"/>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM