【问题标题】:Binding IsEnabled on ComboBoxItem does not work在 ComboBoxItem 上绑定 IsEnabled 不起作用
【发布时间】:2013-09-13 14:53:20
【问题描述】:

我有这个模型:

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,其中一些将Enabled 属性设置为true,而另一些则没有。

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

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

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

【问题讨论】:

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


【解决方案1】:

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

【解决方案2】:

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

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

【讨论】:

  • 不,不幸的是,它并不能解决问题,但感谢您的想法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-19
  • 2017-12-11
  • 1970-01-01
相关资源
最近更新 更多