Nicholas
Nicholas

Reputation: 3336

How do I set my main application theme on a dialog box, without setting it explicitly in the XAML?

Update

I am embarrassed to say that I made a mistake. The error was that my theme doesn't style the elements I was using to test, so of course I wasn't seeing the applied styles.

However, the answers made in response to this question do show how to explicitly set resources from one UserControl to another... which is interesting.

When I set resources on the application in the way I describe below, it does indeed implicitly set all user control themes in the resulting application.


I am using a theme, set as a ResourceDictionary on my main Application class. My main window I guess implicitly uses this theme to style itself.

<Application>
    <Application.Resources>
        <ResourceDictionary Source="Themes/ExpressionDark.xaml" />

The following comments are wrong, and everything is styled implicitly

When I show a dialog however, this is not styled.

DialogBox dialog = new DialogBox();
dialog.ShowDialog();

Is there some way to do this implicitly without specifying the style explicitly in the XAML of DialogBox?

Edit

I tried setting the resources in the following ways. They did not work.

Window main = App.Current.Windows[0];
dialog.Resources = main.Resources;
dialog.Owner = main;

Also tried setting the from the main App... which is where the original resources are defined.

dialog.Resources = App.Current.Resources;

Upvotes: 1

Views: 1358

Answers (2)

ShahidAzim
ShahidAzim

Reputation: 1494

You can implement like as follows:

dialog.Resources.MergedDictionaries.Add( this.Resources.MergedDictionaries[0] );

And the other possibility is to load them from app (but I wouldn't prefer):

ResourceDictionary dict = Application.LoadComponent( new Uri( "<library>;component/Themes/<theme>.xaml", UriKind.Relative ) ) as ResourceDictionary;

dialog.Resources.MergedDictionaries.Add( dict );

Upvotes: 3

brunnerh
brunnerh

Reputation: 185553

You could transfer resources one way or another, or just set the dialogue's resources to the resources of the main window if you don't need to specify dialogue specific resources as well:

    public Dialogue(Window owner)
    {
        this.InitializeComponent();
        Owner = owner;
        Resources = Owner.Resources;
    }

(Since it's a dialogue i set the Owner property as well)

Upvotes: 1

Related Questions