简体   繁体   中英

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;

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM