【问题标题】:Design a combo box with disabled combo-box items [closed]设计一个带有禁用组合框项目的组合框[关闭]
【发布时间】:2013-12-04 21:32:19
【问题描述】:

我想设计一个组合框,它能够禁用其中的组合框项目(绑定到可观察集合)。 我知道这不是最佳的 UI 策略,但我的模块需要它。

【问题讨论】:

标签: c# .net wpf combobox custom-controls


【解决方案1】:

我假设您的模型看起来像这样:

public class Model
{
    public string Text { get; set; }
    public bool Enabled { get; set; }
}

出于演示的目的,我将手动进行绑定:

theComboBox.ItemsSource = new ObservableCollection<Model>{
            new Model{Text="One", Enabled=true},
            new Model{Text="Two", Enabled=false},
            new Model{Text="Three", Enabled=true}
        };

您需要做的就是修改 ComboBoxItem 样式并将 IsEnabled 属性绑定到模型中的 Enable 字段:

<ComboBox Name="theComboBox" DisplayMemberPath="Text" Margin="26,28,302,270">
        <ComboBox.Resources>
            <Style TargetType="{x:Type ComboBoxItem}">
                <Setter Property="IsEnabled" Value="{Binding Enabled}"/>
            </Style>
        </ComboBox.Resources>
    </ComboBox>

显然,如果您的模型中没有“启用”字段,那么您将需要使用转换器或其他东西。

【讨论】:

    【解决方案2】:

    你可以看到这个link

    或者试试这段代码

    <ComboBox ItemTemplate="{StaticResource IpInfoTemplate}" 
              ItemsSource="{Binding Source={x:Static WpfApplication1:App.IpInfoList}, Mode=OneWay}">
        <ComboBox.ItemContainerStyle>
            <Style TargetType="{x:Type ComboBoxItem}">
                <Setter Property="IsEnabled" Value="{Binding Enabled}"/>
            </Style>
        </ComboBox.ItemContainerStyle>
    </ComboBox>
    

    【讨论】:

      猜你喜欢
      • 2017-11-29
      • 1970-01-01
      • 1970-01-01
      • 2010-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-09
      相关资源
      最近更新 更多