wpf 设置组合框的 PART_EditableTextBox 样式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15394578/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Styling a combobox's PART_EditableTextBox
提问by 0xFF
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:
我想将条件格式(只是字体颜色)添加到组合框的文本框部分。根据 MSDN,它是“PART_EditableTextBox”元素。对 SO 的快速搜索让我开始了,但我现在面临一个问题:它覆盖了整个模板。根据这个 SO answer,我可以使用“BasedOn”来覆盖特定的属性,但我不知道如何/在哪里使用它。这是我当前的模板:
<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:
它有效,我仍然可以输入有效值,“MAL”确实使文本变为橙色,但不再有下拉列表。在 MSDN 上,我发现了以下内容:
<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?
我想我的模板应该基于这个“ComboBoxTextBox”,但我不知道如何引用它。我真的需要复制整个模板还是有办法覆盖特定属性?
EDIT: On the same MSDN page comboboxTextBox is defined as
编辑:在同一个 MSDN 页面上,comboboxTextBox 被定义为
<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.
我没有看到覆盖此模板如何删除下拉列表。
采纳答案by failedprogramming
Ok I think I got really confused after reading all of your code and having a really looooooong day at work, I totally missed the point of your question.... which is
好吧,我想我在阅读了您的所有代码并在工作中度过了非常棒的一天后真的很困惑,我完全没有抓住您的问题的重点......这是
I want to add conditionnal formatting (just font color) to the textbox part of a combobox
我想将条件格式(只是字体颜色)添加到组合框的文本框部分
Well if that's all you want to do, then it's really easy with just a simple style trigger. I can achieve that with this xaml.
好吧,如果这就是您想要做的一切,那么只需一个简单的样式触发器就很容易了。我可以用这个 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>
Hope this helps!
希望这可以帮助!

