2010-06-03 34 views
3

Je rencontre un problème avec ContentPresenter se comportant de manière inattendue selon que les styles se trouvent dans la fenêtre. Ressources ou dans un ResourceDictionary. Plus précisément, je définis le premier plan du TextBlock par défaut sur Noir, puis définissez la valeur de premier plan dans mon style de bouton par défaut sur Blanc.WPF: ContentPresenter modifiant de façon inattendue le premier plan en fonction de l'emplacement des styles

Si les styles existe sur la page comme ça, ils fonctionnent très bien:

<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> 

Mais si je déménage ces mêmes styles sur un ResourceDictionary, au premier plan des commutateurs de bouton noir.

Mise à jour MainWindow:

<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> 

Et mon App.xaml parce que quelqu'un vous le demandera:

<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> 

Toute aide serait grandement appréciée :)

Répondre

0

Parlons-nous qu'il se comporte bizarrement au moment du design ou de l'exécution. J'ai eu des problèmes avec le concepteur Visual Studio avant ... Surtout avec le App.Xaml ne fonctionne pas avant que je ne fonctionne.

+0

Désolé, je n'étais probablement pas assez clair sur le problème, c'est à l'exécution. Si les styles sont sur la fenêtre, la couleur du texte du bouton fonctionne comme prévu et est blanche. Une fois que je l'ai déplacé hors de la fenêtre et dans un ResourceDictionary, le texte devient noir. – VLTII

1

Je viens de réaliser que je n'ai jamais répondu à cette question. La version courte est que vous devriez toujours utiliser des étiquettes plutôt que des blocs de texte, car les blocs de texte ne sont pas réellement des contrôles.

+0

merci pour l'info, j'ai eu du mal à changer la couleur de premier plan de mon dataGrid personnalisé – noobie