[英]How to make selecteditem text red and bold on trigger (in combobox)
I faced the problem: if I choose item from my combobox and its property .IsNotCorrect is true then make this selecteditem text red and bold and all other items in combobox are black.我遇到了这个问题:如果我从我的组合框中选择项目并且它的属性 .IsNotCorrect 为真,那么将这个 selecteditem 文本设为红色和粗体,并且组合框中的所有其他项目都是黑色的。 This is my attempt of doing this but nothing happens:这是我这样做的尝试,但没有任何反应:
<ComboBox x:Name="REASON_ID" DisplayMemberPath="Name" IsReadOnly="True" IsEditable="True"
SelectedItem="{Binding SelectedReason, Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}">
<ComboBox.ItemsSource>
<CompositeCollection>
<ComboBoxItem Content="{DynamicResource lang_Common_SelectItem}"
IsEnabled="False"/>
<CollectionContainer
Collection="{Binding Source={StaticResource StaticReasons}}"/>
<Style TargetType="{x:Type ComboBoxItem}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=SelectedItem.IsNotCorrect, ElementName=REASON_ID}" Value="True">
<Setter Property="Foreground" Value="Red" />
<Setter Property="FontWeight" Value="Bold" />
</DataTrigger>
</Style.Triggers>
</Style>
</CompositeCollection>
</ComboBox.ItemsSource>
</ComboBox>
If you want, that all items in drop down list, which IsNotCorrect
are bold and red, then remove your Style from displayed collection and put it to the ComboBox.Resources
.如果需要,下拉列表中的所有项目( IsNotCorrect
都是粗体和红色),然后从显示的集合中删除您的 Style 并将其放入ComboBox.Resources
。 The binding should also be adjusted:绑定也应该调整:
<ComboBox.Resources>
<Style TargetType="{x:Type ComboBoxItem}">
<Style.Triggers>
<DataTrigger Binding="{Binding IsNotCorrect}" Value="True">
<Setter Property="Foreground" Value="Red" />
<Setter Property="FontWeight" Value="Bold" />
</DataTrigger>
</Style.Triggers>
</Style>
</ComboBox.Resources>
If you want to change the representation in text field, then you have to modify the ControlTemplate
of ComboBox
.如果要更改文本字段中的表示,则必须修改ComboBox
的ControlTemplate
。
ControlTemplate
of ComboBox see ComboBox Styles and Templates eg to the Resources
of element, which contains your ComboBox.将 ComboBox 的默认ControlTemplate
(参见ComboBox 样式和模板)复制到包含您的 ComboBox 的元素的Resources
中。<Style x:Key="{x:Type ComboBox}"
to the <Style x:Key="UsrDefinedStyle"
in the copied code.将复制的代码中的<Style x:Key="{x:Type ComboBox}"
更改为<Style x:Key="UsrDefinedStyle"
。TextBox
with name PART_EditableTextBox
and remove Style="{x:Null}"
in the copied code.找到名为PART_EditableTextBox
TextBox
并删除复制代码中的Style="{x:Null}"
。ComboBox
to the Style="{StaticResource UsrDefinedStyle}"
将ComboBox
的Style="{StaticResource UsrDefinedStyle}"
设置为Style="{StaticResource UsrDefinedStyle}"
Put to the Resources
of your ComboBox
:放入ComboBox
的Resources
中:
<Style TargetType="{x:Type TextBox}" BasedOn="{x:Null}"> <Style.Triggers> <DataTrigger Binding="{Binding SelectedItem.IsNotCorrect, RelativeSource={RelativeSource AncestorType=ComboBox}}" Value="True"> <Setter Property="Foreground" Value="Red" /> <Setter Property="FontWeight" Value="Bold" /> </DataTrigger> </Style.Triggers> </Style>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.