Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在触发器(组合框中)上使selecteditem文本变为红色和粗体_C#_Wpf_Xaml_Combobox_Selecteditem - Fatal编程技术网

C# 如何在触发器(组合框中)上使selecteditem文本变为红色和粗体

C# 如何在触发器(组合框中)上使selecteditem文本变为红色和粗体,c#,wpf,xaml,combobox,selecteditem,C#,Wpf,Xaml,Combobox,Selecteditem,我面临的问题是:如果我从combobox及其属性中选择项。IsNotCorrect为true,则将此selecteditem文本设为红色和粗体,combobox中的所有其他项均为黑色。这是我的尝试,但什么都没有发生: <ComboBox x:Name="REASON_ID" DisplayMemberPath="Name" IsReadOnly="True" IsEditable="True" SelectedItem="{Binding SelectedReason, Mode

我面临的问题是:如果我从combobox及其属性中选择项。IsNotCorrect为true,则将此selecteditem文本设为红色和粗体,combobox中的所有其他项均为黑色。这是我的尝试,但什么都没有发生:

<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> 

如果需要,请确保下拉列表中
不正确的所有项目都是粗体和红色的,然后从显示的集合中删除样式并将其放入组合框。参考资料
。还应调整绑定:

<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>

不清楚您想要什么。下拉列表中的所有项目,如果
不正确且为红色,或者如果所选项目
不正确且为红色,则rop下拉列表中的所有项目均为红色,或者?@Rekshino,如果我从我的组合框及其属性中选择了项目。IsNotCorrect为真,则将此selecteditem文本设置为红色和粗体(组合框中的所有其他项目不变)下拉列表中的项目应该是红色的还是文本字段中的表示?@Rekshino,仅文本字段中的表示请参阅更新的答案。非常感谢,这似乎是正确的答案,但仍然无法将其应用于我的代码()试着执行我写的所有步骤。我做了所有这些,从制作一个默认的ControlTemplate到在我的组合框中放入一部分代码,请看我的另一个问题,不要得到我遗漏的“在我的组合框中放入一部分代码”你必须将所有代码从MS页面复制到
参考资料中。在你发布的另一个问题中,遗漏了一半以上。
<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>