Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.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
Wpf 组合框中的样式设备_Wpf - Fatal编程技术网

Wpf 组合框中的样式设备

Wpf 组合框中的样式设备,wpf,Wpf,我有一个简单的窗口: <Window.Resources> <Style TargetType="{x:Type TextBlock}"> <Setter Property="Foreground" Value="Red"/> </Style> </Window.Resources> <StackPanel> <ComboBox VerticalAlignment="Top"

我有一个简单的窗口:

<Window.Resources>
    <Style TargetType="{x:Type TextBlock}">
        <Setter Property="Foreground" Value="Red"/>
    </Style>
</Window.Resources>

<StackPanel>
    <ComboBox VerticalAlignment="Top" HorizontalAlignment="Left">
        <ComboBoxItem IsSelected="True" Content="1"/>
        <ComboBoxItem Content="2"/>
        <ComboBoxItem Content="3"/>
    </ComboBox>
</StackPanel>

但此样式不适用于combobox。如果我将其移动到app.xaml,它将开始工作。
有人能解释一下这种奇怪的行为吗?

App.xaml中的样式应用于模板内部,这意味着如果ComboBoxItem在内部创建一个只受这些全局样式影响的文本块

要更改文本颜色,您应该设置
前景
或组合框本身(您也可以使用样式)。

您可以这样做

    <Window.Resources>
<Style TargetType="{x:Type Control}" x:Key="TextStyle">
            <Setter Property="Foreground" Value="Red"/>
        </Style>
        <Style TargetType="{x:Type ComboBox}" BasedOn="{StaticResource TextStyle}">            
        </Style>
    </Window.Resources>


奇怪,但您的targettype是TextBlock。它应该是组合框。但为什么你把它放在app.xaml中会起作用对我来说是个谜。我希望它在那里也不会起作用。是的,它解释道。您可以提供更多详细说明此行为的链接吗?模板内的控件不在资源查找的一个阶段中使用的逻辑树中,但查找直接检查应用程序资源,可能可以更好地解释它。