Remplacer le style par défaut dans WPF TextBox, basé sur PresentationFramework.Aero

StackOverflow https://stackoverflow.com/questions/643440

  •  22-07-2019
  •  | 
  •  

Question

Je souhaite utiliser le style de zone de texte Aero, tout en remplaçant certaines propriétés. J'essaie d'accomplir cela en:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="/PresentationFramework.Aero, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, ProcessorArchitecture=MSIL;component/themes/aero.normalcolor.xaml" />
    </ResourceDictionary.MergedDictionaries>

    <Style x:Key="{x:Type TextBox}" TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
        <Setter Property="Margin" Value="2" />
        <Setter Property="Padding" Value="2" />
    </Style>
</ResourceDictionary>

Toutefois, cela entraîne une StackOverflowException lors du démarrage de mon application. Lorsque je supprime la référence à PresentationFramework.Aero, cela fonctionne, mais j'obtiens le style par défaut du système d'exploitation, ce qui rend l'application moche. ;)

Donc, en effet: si je veux remplacer un style sur tous mes champs de texte, je ne peux pas obtenir le look Aero. Si je veux le look Aero, je ne peux remplacer aucun style. Impasse.

Avez-vous un moyen de résoudre ce problème?

Était-ce utile?

La solution

Cela semble fonctionner si vous définissez le Style en tant que ressource de niveau inférieur au lieu d'être dans le même ResourceDictionary:

<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/PresentationFramework.Aero, Version=3.0.0.0, Culture=Neutral, PublicKeyToken=31bf3856ad364e35, ProcessorArchitecture=MSIL;component/themes/aero.normalcolor.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Grid.Resources>
    <Border BorderBrush="Blue" BorderThickness="3">
        <Border.Resources>
            <Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
                <Setter Property="Margin" Value="2" />
                <Setter Property="Padding" Value="2" />
            </Style>
        </Border.Resources>
        <TextBox />
    </Border>
</Grid>

Autres conseils

Contrairement au code dans la réponse acceptée, celui-ci permet d'utiliser le dictionnaire de ressources pour les styles. Volé sans vergogne dans http: //social.msdn. microsoft.com/forums/en-US/wpf/thread/3c66adb7-fd26-40c7-8404-85f6fefbd392/ répondu par Vivien Ruitz

<!--App.xaml-->
        <ResourceDictionary.MergedDictionaries> 
            <ResourceDictionary Source="/MyAppli;component/Resources/Themes/StyleDictionary.xaml"/>  
            <ResourceDictionary Source="/MyAppli;component/Resources/Themes/ApplyStyleDictionary.xaml"/>  
            ...  
        </ResourceDictionary.MergedDictionaries> 

<!--StyleDictionary.xaml-->
        <ResourceDictionary.MergedDictionaries> 
            <ResourceDictionary Source="/PresentationFramework.Aero;V3.0.0.0;31bf3856ad364e35;component/themes/aero.normalcolor.xaml" /> 
        </ResourceDictionary.MergedDictionaries> 
        <Style x:Key="ButtonStyleToApply" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}" > 
            ...  <!--Extend the aero style here-->
        </Style> 

<!--ApplyStyleDictionary.xaml-->
        <Style TargetType="Button" BasedOn="{StaticResource ButtonStyleToApply}"/>
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top