如何在触发器上使selecteditem文本变为红色和粗体(在组合框中) [英] How to make selecteditem text red and bold on trigger (in combobox)
问题描述
我遇到了一个问题:如果我从组合框中选择项目,并且其属性.IsNotCorrect为true,则使此selecteditem文本变为红色和粗体,而组合框中的所有其他项目均为黑色。这是我尝试执行的操作,但没有任何反应:
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. 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>
推荐答案
如果需要,下拉列表中的所有项目,其中 IsNotCorrect
为粗体和红色,然后从显示的集合中删除您的样式,并将其放入 ComboBox.Resources
。绑定也应调整:
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
. 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>
如果要更改文本字段中的表示形式,则必须修改
。 ComboBox
的ControlTemplate
If you want to change the representation in text field, then you have to modify the ControlTemplate
of ComboBox
.
- 复制ComboBox的默认
ControlTemplate
参见组合框样式
和
模板
例如到资源
元素,其中包含您的ComboBox。 - 更改
< Style x:Key =在复制的代码中将<{x:Type ComboBox}
放入< Style
。
x:Key = UsrDefinedStyle - 查找名称为
PART_EditableTextBox
的TextBox
,并删除Style = {x:Null}
。 - 设置
ComboBox的样式
到Style = {StaticResource UsrDefinedStyle}
-
放入
ComboBox
中的资源:
- copy the default
ControlTemplate
of ComboBox see ComboBox Styles and Templates e.g. to theResources
of element, which contains your ComboBox. - Change the
<Style x:Key="{x:Type ComboBox}"
to the<Style x:Key="UsrDefinedStyle"
in the copied code. - Find
TextBox
with namePART_EditableTextBox
and removeStyle="{x:Null}"
in the copied code. - Set style of your
ComboBox
to theStyle="{StaticResource UsrDefinedStyle}"
Put to the
Resources
of yourComboBox
:
<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>
这篇关于如何在触发器上使selecteditem文本变为红色和粗体(在组合框中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!