在组合框覆盖默认的TextBlock风格 [英] Override default TextBlock style in ComboBox

查看:98
本文介绍了在组合框覆盖默认的TextBlock风格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在App.xaml中,定义了默认的TextBlock风格,似乎也影响到组合框项目的文本颜色。现在,我该如何明确设置在我的主窗口中定义的组合框的文本颜色? (我想保持默认的样式,但组合框的文字颜色,也就是说,蓝色,而不是红色...)



的App.xaml

 <应用X:类=WpfApplication1.App
的xmlns =HTTP:/ /schemas.microsoft.com/winfx/2006/xaml/presentation
的xmlns:X =http://schemas.microsoft.com/winfx/2006/xaml
的StartupUri =MainWindow.xaml >
< Application.Resources>
<风格的TargetType ={X:类型的TextBlock}>
< setter属性=前景VALUE =红/>
< /样式和GT;
< /Application.Resources>





MainWindow.xaml

 <窗口x:类=WpfApplication1.MainWindow
的xmlns =HTTP:/ /schemas.microsoft.com/winfx/2006/xaml/presentation
的xmlns:X =http://schemas.microsoft.com/winfx/2006/xaml
标题=主窗口> ;
<网格和GT;
<组合框名称=comboBox1的SelectedIndex =0的Horizo​​ntalAlignment =左VerticalAlignment =评出的>
< ComboBoxItem CONTENT =项目1/>
< ComboBoxItem CONTENT =项目2/>
< ComboBoxItem CONTENT =项目3/>
< /组合框>
< /网格和GT;





事情我已经试过:




  1. 设置Combobox.Foreground

  2. 设置TextElement.Foreground

  3. 设置TextBlock.Foreground

  4. 定义隐含的另一个风格的TextBlock在ComboBox.Resources

  5. 定义隐含的另一个风格的TextBlock中Grid.Resources

  6. 定义隐含的另一个风格的TextBlock在Window.Resources


解决方案

最含蓄的TextBlock风格将停止在控制边界,除非你把它们放在 Application.Resources



例如,把你的风格 Window.Resources 将使它适用于所有< TextBlock的> 的对象,但不包括其他控制模板内的文本,如组合框按钮



我会建议将您的风格融入 Window.Resources ,然后你的造型组合框的项目有任何你想要的前景色。

 < ComboBox.Resources> 
<风格的TargetType ={X:类型ComboBoxItem}>
< setter属性=前景VALUE =蓝/>
< /样式和GT;
< /ComboBox.Resources>

如果你想保持它在 Application.Resources ,那么我怀疑你需要跟踪什么 X:静态刷键用于设置 TextBlock.Text 颜色和覆盖在你的 ComboBox.Resources


I have a default TextBlock style defined in App.xaml, which seems to also affect the text color of ComboBox items. Now, how do I explicitly set the text color of a ComboBox defined in my main window? (I'd like to keep the default style, but have the combobox text color as, say, blue instead of red...)

App.xaml

<Application x:Class="WpfApplication1.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         StartupUri="MainWindow.xaml">
<Application.Resources>
    <Style TargetType="{x:Type TextBlock}">
        <Setter Property="Foreground" Value="Red" />
    </Style>
</Application.Resources>

MainWindow.xaml

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow">
<Grid>
    <ComboBox Name="comboBox1" SelectedIndex="0" HorizontalAlignment="Left" VerticalAlignment="Top">
        <ComboBoxItem Content = "Item1"/>
        <ComboBoxItem Content = "Item2"/>
        <ComboBoxItem Content = "Item3"/>
    </ComboBox>
</Grid>

Things I've tried:

  1. Set Combobox.Foreground
  2. Set TextElement.Foreground
  3. Set TextBlock.Foreground
  4. Define another implicit TextBlock style in ComboBox.Resources
  5. Define another implicit TextBlock style in Grid.Resources
  6. Define another implicit TextBlock style in Window.Resources

解决方案

Most implicit TextBlock styles will stop at control boundaries, unless you put them in Application.Resources

For example, placing your style in Window.Resources will make it apply to all <TextBlock> objects, but not to text inside other Control Templates such as a ComboBox or a Button

I would suggest moving your style into Window.Resources, and then styling your ComboBox items to have whatever foreground color you want.

<ComboBox.Resources>
    <Style TargetType="{x:Type ComboBoxItem}">
        <Setter Property="Foreground" Value="Blue" />
    </Style>
</ComboBox.Resources>

If you want to keep it in Application.Resources, then I suspect you need to track down what x:Static brush key is used for setting the TextBlock.Text color and overwrite that in your ComboBox.Resources

这篇关于在组合框覆盖默认的TextBlock风格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆