1

I want to add conditionnal formatting (just font color) to the textbox part of a combobox. According to MSDN, it's the "PART_EditableTextBox" element. A quick search on SO got me started but I now face a problem: it overrides the whole template. According to this SO answer, I can use "BasedOn" to override only specific properties but I've no idea how/where to use it. This is my current template:

    <ControlTemplate x:Key="MyComboBoxTextBox" TargetType="ComboBox" <!--Here?--> >
        <TextBox x:Name="PART_EditableTextBox" <!--Maybe Here?-->>
            <TextBox.Style>
                <Style TargetType="TextBox">
                    <Style.Triggers>
                        <Trigger Property="Text" Value="MAL">
                            <Setter Property="Foreground" Value="DarkOrange"></Setter>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </TextBox.Style>
        </TextBox>
    </ControlTemplate>

It works, I can still type in valid values and "MAL" does make the text orange but there's no dropdown anymore. On MSDN, I found the following:

<TextBox x:Name="PART_EditableTextBox"
               Style="{x:Null}"
               Template="{StaticResource ComboBoxTextBox}"
               HorizontalAlignment="Left"
               VerticalAlignment="Bottom"
               Margin="3,3,23,3"
               Focusable="True"
               Background="Transparent"
               Visibility="Hidden"
               IsReadOnly="{TemplateBinding IsReadOnly}" />

I suppose I should base my template on this "ComboBoxTextBox" but I don't know how to reference it. Do I really need to copy the whole template or is there a way to override a specific property?

EDIT: On the same MSDN page comboboxTextBox is defined as

<ControlTemplate x:Key="ComboBoxTextBox"
                 TargetType="{x:Type TextBox}">
       <Border x:Name="PART_ContentHost"
          Focusable="False"
          Background="{TemplateBinding Background}" />
</ControlTemplate>

I don't see how overriding this template removes the dropdown list.

4

1 回答 1

0

好的,我想在阅读了您的所有代码并在工作中度过了非常漫长的一天后,我感到非常困惑,我完全错过了您的问题的重点....

我想将条件格式(只是字体颜色)添加到组合框的文本框部分

好吧,如果这就是您想做的所有事情,那么只需一个简单的样式触发器就很容易了。我可以用这个 xaml 来实现。

<ComboBox HorizontalAlignment="Center" VerticalAlignment="Center">
    <ComboBox.Resources>
        <Style TargetType="ComboBox">
            <Style.Triggers>
                <Trigger Property="Text" Value="MAL">
                    <Setter Property="Foreground" Value="DarkOrange" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </ComboBox.Resources>
    <ComboBoxItem>MAL</ComboBoxItem>
    <ComboBoxItem>1</ComboBoxItem>
    <ComboBoxItem>2</ComboBoxItem>
    <ComboBoxItem>3</ComboBoxItem>
</ComboBox>

希望这可以帮助!

于 2013-03-14T11:06:06.853 回答