【发布时间】:2013-12-04 21:32:19
【问题描述】:
我想设计一个组合框,它能够禁用其中的组合框项目(绑定到可观察集合)。 我知道这不是最佳的 UI 策略,但我的模块需要它。
【问题讨论】:
-
请仔细阅读this post。
标签: c# .net wpf combobox custom-controls
我想设计一个组合框,它能够禁用其中的组合框项目(绑定到可观察集合)。 我知道这不是最佳的 UI 策略,但我的模块需要它。
【问题讨论】:
标签: c# .net wpf combobox custom-controls
我假设您的模型看起来像这样:
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>
显然,如果您的模型中没有“启用”字段,那么您将需要使用转换器或其他东西。
【讨论】:
你可以看到这个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>
【讨论】: