WPF:ContentPresenter意外地根据样式所在的位置更改Foreground

问题描述:

我遇到的问题是,根据样式是否位于窗口中,ContentPresenter的行为意外。资源或ResourceDictionary中。具体来说,我将默认TextBlock的前景设置为黑色,然后将默认按钮样式中的前景值设置为白色。WPF:ContentPresenter意外地根据样式所在的位置更改Foreground

如果存在样式像这样的页面上,他们很好地工作:

<Window 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero" 
x:Class="TestBed.MainWindow" 
x:Name="Window" 
Title="MainWindow" 
Width="640" Height="480"> 
<Window.Resources> 
    <Style TargetType="{x:Type TextBlock}"> 
    <Setter Property="Foreground" Value="Black" /> 
    </Style> 
    <Style x:Key="ButtonFocusVisual"> 
    <Setter Property="Control.Template"> 
    <Setter.Value> 
    <ControlTemplate> 
     <Rectangle Stroke="Black" StrokeDashArray="1 2" StrokeThickness="1" Margin="2" SnapsToDevicePixels="true"/> 
    </ControlTemplate> 
    </Setter.Value> 
    </Setter> 
    </Style> 
    <LinearGradientBrush x:Key="ButtonNormalBackground" EndPoint="0,1" StartPoint="0,0"> 
    <GradientStop Color="#F3F3F3" Offset="0"/> 
    <GradientStop Color="#EBEBEB" Offset="0.5"/> 
    <GradientStop Color="#DDDDDD" Offset="0.5"/> 
    <GradientStop Color="#CDCDCD" Offset="1"/> 
    </LinearGradientBrush> 
    <SolidColorBrush x:Key="ButtonNormalBorder" Color="#FF707070"/> 
    <Style TargetType="{x:Type Button}"> 
    <Setter Property="FocusVisualStyle" Value="{StaticResource ButtonFocusVisual}"/> 
    <Setter Property="Background" Value="{StaticResource ButtonNormalBackground}"/> 
    <Setter Property="BorderBrush" Value="{StaticResource ButtonNormalBorder}"/> 
    <Setter Property="BorderThickness" Value="1"/> 
    <Setter Property="Foreground" Value="White"/> 
    <Setter Property="HorizontalContentAlignment" Value="Center"/> 
    <Setter Property="VerticalContentAlignment" Value="Center"/> 
    <Setter Property="Padding" Value="1"/> 
    <Setter Property="Template"> 
    <Setter.Value> 
    <ControlTemplate TargetType="{x:Type Button}"> 
     <Microsoft_Windows_Themes:ButtonChrome x:Name="Chrome" SnapsToDevicePixels="true" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" RenderDefaulted="{TemplateBinding IsDefaulted}" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderPressed="{TemplateBinding IsPressed}"> 
     <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" RecognizesAccessKey="True"/> 
     </Microsoft_Windows_Themes:ButtonChrome> 
     <ControlTemplate.Triggers> 
     <Trigger Property="IsKeyboardFocused" Value="true"> 
     <Setter Property="RenderDefaulted" TargetName="Chrome" Value="true"/> 
     </Trigger> 
     <Trigger Property="ToggleButton.IsChecked" Value="true"> 
     <Setter Property="RenderPressed" TargetName="Chrome" Value="true"/> 
     </Trigger> 
     <Trigger Property="IsEnabled" Value="false"> 
     <Setter Property="Foreground" Value="#ADADAD"/> 
     </Trigger> 
     </ControlTemplate.Triggers> 
    </ControlTemplate> 
    </Setter.Value> 
    </Setter> 
    </Style> 
</Window.Resources> 
<StackPanel x:Name="LayoutRoot"> 
    <Button Content="Button" /> 
</StackPanel> 
</Window> 

但如果我搬完相同的样式到ResourceDictionary中,该按钮会变为黑色的前景。

更新主窗口:

<Window 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero" 
x:Class="TestBed.MainWindow" 
x:Name="Window" 
Title="MainWindow" 
Width="640" Height="480"> 

<StackPanel x:Name="LayoutRoot"> 
    <Button Content="Button" /> 
</StackPanel> 
</Window> 

的ResourceDictionary:

<ResourceDictionary 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
mc:Ignorable="d"> 

    <Style TargetType="{x:Type TextBlock}"> 
    <Setter Property="Foreground" Value="Black" /> 
    </Style> 
    <Style x:Key="ButtonFocusVisual"> 
    <Setter Property="Control.Template"> 
    <Setter.Value> 
    <ControlTemplate> 
     <Rectangle Stroke="Black" StrokeDashArray="1 2" StrokeThickness="1" Margin="2" SnapsToDevicePixels="true"/> 
    </ControlTemplate> 
    </Setter.Value> 
    </Setter> 
    </Style> 
    <LinearGradientBrush x:Key="ButtonNormalBackground" EndPoint="0,1" StartPoint="0,0"> 
    <GradientStop Color="#F3F3F3" Offset="0"/> 
    <GradientStop Color="#EBEBEB" Offset="0.5"/> 
    <GradientStop Color="#DDDDDD" Offset="0.5"/> 
    <GradientStop Color="#CDCDCD" Offset="1"/> 
    </LinearGradientBrush> 
    <SolidColorBrush x:Key="ButtonNormalBorder" Color="#FF707070"/> 
    <Style TargetType="{x:Type Button}"> 
    <Setter Property="FocusVisualStyle" Value="{StaticResource ButtonFocusVisual}"/> 
    <Setter Property="Background" Value="{StaticResource ButtonNormalBackground}"/> 
    <Setter Property="BorderBrush" Value="{StaticResource ButtonNormalBorder}"/> 
    <Setter Property="BorderThickness" Value="1"/> 
    <Setter Property="Foreground" Value="White"/> 
    <Setter Property="HorizontalContentAlignment" Value="Center"/> 
    <Setter Property="VerticalContentAlignment" Value="Center"/> 
    <Setter Property="Padding" Value="1"/> 
    <Setter Property="Template"> 
    <Setter.Value> 
    <ControlTemplate TargetType="{x:Type Button}"> 
     <Microsoft_Windows_Themes:ButtonChrome x:Name="Chrome" SnapsToDevicePixels="true" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" RenderDefaulted="{TemplateBinding IsDefaulted}" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderPressed="{TemplateBinding IsPressed}"> 
     <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" RecognizesAccessKey="True"/> 
     </Microsoft_Windows_Themes:ButtonChrome> 
     <ControlTemplate.Triggers> 
     <Trigger Property="IsKeyboardFocused" Value="true"> 
     <Setter Property="RenderDefaulted" TargetName="Chrome" Value="true"/> 
     </Trigger> 
     <Trigger Property="ToggleButton.IsChecked" Value="true"> 
     <Setter Property="RenderPressed" TargetName="Chrome" Value="true"/> 
     </Trigger> 
     <Trigger Property="IsEnabled" Value="false"> 
     <Setter Property="Foreground" Value="#ADADAD"/> 
     </Trigger> 
     </ControlTemplate.Triggers> 
    </ControlTemplate> 
    </Setter.Value> 
    </Setter> 
    </Style> 
</ResourceDictionary> 

而且我的App.xaml,因为有人会问它:

<Application 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
x:Class="TestBed.App" 
StartupUri="MainWindow.xaml"> 
<Application.Resources> 
    <!-- Resources scoped at the Application level should be defined here. --> 
    <ResourceDictionary> 
    <ResourceDictionary.MergedDictionaries> 
    <ResourceDictionary Source="ResourceDictionary.xaml"/> 
    </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</Application.Resources> 
</Application> 

任何帮助将不胜感激:)

我们是否在说它的行为奇怪的设计时间或运行时间。我之前遇到过Visual Studio设计师的问题......特别是在我运行之前,App.Xaml没有工作。

+0

对不起,我可能不太清楚这个问题 - 这是在运行时。如果样式位于窗口上,则该按钮的文本颜色按预期工作并且是白色的。一旦我将它移出窗口并放入ResourceDictionary中,文本就会变黑。 – VLTII 2010-06-03 19:25:39

刚才意识到我从来没有回答过这个问题。简短版本是你应该总是使用标签而不是文本块,因为文本块实际上不是控件。

+0

感谢您的信息,我一直在努力改变我的自定义数据网格的前景色 – noobie 2013-06-26 09:39:22