[英]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.