Ramin Bateni
Ramin Bateni

Reputation: 17415

WPF MahApps Change style of Window notworking for elements inside its UserControl

I added MahApps resources like in App.xaml:

    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>

            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.AnimatedTabControl.xaml" />

            <!-- accent resource -->
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/blue.xaml" />

            <!-- theme resource -->
            <!-- change "BaseLight" to the theme you want -->
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/basedark.xaml" />              

        </ResourceDictionary.MergedDictionaries>
        ....

It works well on all of my windows but i have a special window (WindowA) that i want be in a different color so i added the MahApps resources to this window

WindowA.xaml:

    <controls:MetroWindow.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>

            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.AnimatedTabControl.xaml" />

            <!-- accent resource -->
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/blue.xaml" />

            <!-- theme resource -->
            <!-- change "BaseLight" to the theme you want -->
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/basedark.xaml" />              

        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
    </controls:MetroWindow.Resources>

Now i want apply my accent color dynamically to it, so i use some codes like this:

ThemeManager.ChangeAppStyle(appOrWindow,
         ThemeManager.GetAccent("Amber"),
         ThemeManager.GetAppTheme("basedark"));

The result is cool for title bar color (Amber color) of the WindowA BUT i have a label (Lable 1) in a UserControl (TestUserControl) inside WindowA and its color is Blue yet!

WindowA > TestUserControl > Lable 1

enter image description here

The lable 1 xaml tag inside TestUserControl:

 <Label Foreground="{StaticResource AccentColorBrush}">Lable1</Label>

I want change all element colors with {StaticResource AccentColorBrush} Foreground color to Amber include all elements inside UserControls of the WindowA with AccentColorBrush StaticResource.

I think the UserControl is using MahApps resources declared in App.xaml. How can i force it to use MahApps resources declared in WindowA.xaml.

How can i fix this?


Edit1

If i apply an accent like Red to the Application the label 1 color will be changed to the Red color.

Upvotes: 0

Views: 1918

Answers (2)

punker76
punker76

Reputation: 14611

You should use DynamicResource instead StaticResource for the foreground.

<Label Foreground="{DynamicResource AccentColorBrush}">Lable1</Label>

Hope that helps.

Upvotes: 0

Glen Thomas
Glen Thomas

Reputation: 10744

Add this to the UserControl.Resources

<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/amber.xaml" />

Upvotes: 1

Related Questions