How to redefine a style in a specific topic

I am trying to override the default style of a basic control (TextBox, ComboBox) in a theme file. Like this:

in topics /classic.xaml

<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
    <Setter Property="Background" Value="Black"/>
</Style>

in topics /Aero.NormalColor.xaml

<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
    <Setter Property="Background" Value="Green"/>
</Style>

But this does not seem to work. I always get the default style unchanged. I even tried it with a specific key as

<Style x:Key="DefaultTextBoxStyle" TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
    <Setter Property="Background" Value="Green"/>
</Style>

and always use this key when declaring a control in xaml. Nothing seems to work.

If I put the style in the application.xaml file, I have no problem, but I want this style to be theme dependent. By the way, it works well with my own users.

Can someone tell me what I'm doing wrong here?

, , , somesort, xaml.

+3
3

-? .

<Application
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="Test.App">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Simple Styles.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

, x: Key , , .

+1

, ThemeInfo , . , . , , ThemeDictionary. A ThemeDictionary ResourceDictionary .

- , , , .

+1

, . , XXX, , .

"", , :

http://blog.catenalogic.com/post/2009/07/20/Override-or-customize-WPF-themes-such-as-PresentationFrameworkAero.aspx

The trick is to define a style in the same resource dictionary with the key "DefaultTextBoxStyle", and then programmatically add styles with the right key ("{x: Type TextBox}" to the main resource of the application. This way you avoid circular references.

+1
source

Source: https://habr.com/ru/post/1784819/


All Articles