简体   繁体   English

如何在对话框中设置主应用程序主题,而不在XAML中明确设置?

[英]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. 但是,回答这个问题的答案确实显示了如何将资源从一个UserControl显式设置为另一个...这很有趣。

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. 我正在使用一个主题,在我的主Application类上设置为ResourceDictionary。 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? 有没有办法隐式地执行此操作而不在DialogBox的XAML中明确指定样式?

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. 还尝试从主App中设置...这是定义原始资源的位置。

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): 另一种可能性是从app加载它们(但我不喜欢):

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) (因为它是对话我也设置了Owner属性)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过对话框将图像上传到 SQLite 数据库,并将图像输出到 XAML? - How do I upload images to the SQLite database, via a dialog box, and output the image to XAML? 如何设置对话框到cursor的position? - How do I set a dialog box to the position of the cursor? 如何在不需要地铁重音主题的情况下显示对话框? - How to show dialog box without needing a metro accent theme? 如何在应用程序外部的窗口上设置“始终在顶部”标志/设置? - How do I set the 'always on top' flag/setting on a window that is external to my application? 我的vs2010中没有“应用程序文件”对话框 - I do not have Application Files dialog box in my vs2010 当用户更改对话框的大小时,如何设置wpf文本框以自动调整大小? - How do I set a wpf text box to resize automatically when the user changes the size of dialog box? 如何在设置属性之前将属性绑定到我操纵的xaml? - How do I bind a property to my xaml that I manipulate before setting the property? 如何在XAML中设置DataGrid的ItemsSource? - How do I set the ItemsSource of a DataGrid in XAML? 如何设置在VS2010中启动Windows窗体应用程序时加载的窗体? (将启动对象设置为无效) - How do I set the form that loads on startup of my Windows Forms application in VS2010? (Setting startup object to no avail) 在c#/ Xaml中启动应用程序时添加一个对话框 - Add a dialog box on application start up in c# / Xaml
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM