2015-10-14 90 views
2

我在我的全局字典中将TextBlock的前景设置为白色,并且此样式也适用于Combobox,这也使得组合框选项无法读取。我无法找到一种简单的方式来为0123或980以上的windows 8或更高版本设置背景。所以我决定现在可以采用默认的Combobox风格,但我仍然无法找到一个简单的解决方案。我试图设置的Combobox的​​,风格TextBlockCombobox.Resources和风格ComboBoxItem没有运气下。最简单的方法来设置组合框的前景色

这将是理想的,如果我可以设置组合框的背景为黑色,而无需复制整个Combobox的控制模板。如果不是的话,如果我可以在我的字典中使用Combobox的前景,那么这将是最好的,但我愿意接受解决方案。

编辑:最低例子包括

Dictionary1.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:local="clr-namespace:WpfApplication1"> 
    <Style TargetType="TextBlock"> 
     <Setter Property="Foreground" Value="WhiteSmoke"/> 
    </Style> 
</ResourceDictionary> 

的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" 
      xmlns:local="clr-namespace:WpfApplication1" 
      StartupUri="MainWindow.xaml"> 
    <Application.Resources> 
     <ResourceDictionary> 
      <ResourceDictionary.MergedDictionaries> 
       <ResourceDictionary Source="Dictionary1.xaml"/> 
      </ResourceDictionary.MergedDictionaries> 
     </ResourceDictionary> 
    </Application.Resources> 
</Application> 

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" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:local="clr-namespace:WpfApplication1" 
     mc:Ignorable="d" 
     Title="MainWindow" Height="350" Width="525" 
     Closing="Window_Closing"> 
    <Grid> 
     <ComboBox Text="test" Height="50" Width="100" Foreground="Black"> 
      <ComboBox.Resources> 
       <Style TargetType="ComboBoxItem"> 
        <Style.Setters> 
         <Setter Property="Foreground" Value="Black"/> 
        </Style.Setters> 
       </Style> 

       <Style TargetType="TextBlock"> 
        <Style.Setters> 
         <Setter Property="Foreground" Value="Black"/> 
        </Style.Setters> 
       </Style> 
      </ComboBox.Resources> 
      <ComboBoxItem>test1</ComboBoxItem> 
      <ComboBoxItem>test2</ComboBoxItem> 
     </ComboBox> 
    </Grid> 
</Window> 

注意,所有的foregroun d =黑色作品。我使用Windows 10

+0

你可以请你发布你的ComboBox和TextBlock模板吗? –

+0

@DaxPandhi我没有模板他们(使用默认的),只有一种样式适用于TextBlock,它是 Steve

+0

我刚才遇到了同样的问题。我会找到我用来修复它的代码并在几分钟内发布。 –

回答

2

这里的问题是,你只能在控制应用的隐式风格与范围不受限制。对于其他元素,范围仅限于DataTemplate或ControlTemplate。这里隐含的风格是什么Resources定义,也没有规定x:Key(其实x:Key将被隐式设置为TargetType的)。

您在此定位的TextBlockComboBox的模板范围内的一个,因此它不能应用于某些隐含的Style或某种来自外部的继承样式。如果没有任何其他覆盖,则应用资源中定义的全局样式可以对所有元素应用样式。

所以,你有一些选择这里。你可以重新模板ComboBoxItem,但它需要更多的代码。如果这不是一种选择,你可以尝试申请的TextBlock全球的风格有些触发,这样的事情:

<Setter Property="Foreground" Value="WhiteSmoke"/>   
<Style.Triggers> 
    <DataTrigger Binding="{Binding IsEnabled, 
       RelativeSource={RelativeSource AncestorType=ComboBox}}" Value="True"> 
     <Setter Property="Foreground" Value="Black"/> 
    </DataTrigger> 
</Style.Triggers> 

Trigger听的ComboBox任何父,如果它的启用,TextBlock将有另一种风格(其中Foreground设置为Black)。当然,如你所见,Trigger是ResourceDictionary中定义的样式。所有其他TextBlock s仍具有其全局样式(Foreground设置为WhiteSmoke)。

无论如何,您需要覆盖应用程序资源中或同一级别中ResoureDictionary中定义的样式。 Trigger只是一种可以应用/覆盖的过滤方式。

+1

很好的伎俩,我可以看到很多地方我可能会重新应用这个技巧。 – Steve

0

如果只是在组合框显示文本:

<Style TargetType="{x:Type ComboBox}"> 
     <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}" /> 
    </Style> 

如果你想改变的背景下,你需要创建一个的ControlTemplate副本。在官方的Win8和更高版本的模板中,存在一个问题 - 不是在ComboBox中,而是在ToggleButton原语中。这里是控制链:ComboBoxTemplate> ToggleButton> ToggleButtonTemplate。但是,背景和其他画笔的TemplateBinding不在TogglebuttonTemplate中,因此对背景的任何更改都不会传播到ToggleButton的子项。唯一的方法是创建一个控件的副本,并通过TemplateBinding ToggleButton模板将缺失的部分连接起来。但是,我不确定ComboBox是否会在其他版本的Windows上保留本机操作系统的外观。

我试图重写切换按钮原始,但它并没有对ComboBox的切换按钮使用自定义模板。

+0

我已经试过这个,很不幸,它不起作用 – Steve

+0

我在尝试别的。我假设组合不可编辑。 –

+0

这很愚蠢,你可能已经尝试过了,但是如何修改Combo的'Foreground'(或绑定)到Black或其他?不得不问。 :) –

相关问题