如何在对话框中设置主应用程序主题,而不在 XAML 中显式设置?
更新
我很不好意思地说我犯了一个错误。错误是我的主题没有设置我用来测试的元素的样式,所以我当然没有看到应用的样式。
然而,针对这个问题的答案确实显示了如何将资源从一个 UserControl 显式设置到另一个......这很有趣。
当我按照下面描述的方式在应用程序上设置资源时,它确实隐式地在生成的应用程序中设置了所有用户控件主题。
我正在使用一个主题,在我的主应用程序类上设置为 ResourceDictionary。我想我的主窗口隐含地使用这个主题来设计自己的风格。
<Application>
<Application.Resources>
<ResourceDictionary Source="Themes/ExpressionDark.xaml" />
以下注释是错误的,并且所有内容都隐式设置了样式
但是,当我显示对话框时,这没有设置样式。
DialogBox dialog = new DialogBox();
dialog.ShowDialog();
有没有某种方法可以隐式执行此操作,而无需在 DialogBox 的 XAML 中显式指定样式?
编辑
我尝试通过以下方式设置资源。他们没有工作。
Window main = App.Current.Windows[0];
dialog.Resources = main.Resources;
dialog.Owner = main;
还尝试从主应用程序设置...这是定义原始资源的地方。
dialog.Resources = App.Current.Resources;
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;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以按如下方式实现:
另一种可能性是从应用程序加载它们(但我不喜欢):
You can implement like as follows:
And the other possibility is to load them from app (but I wouldn't prefer):
您可以以一种或另一种方式传输资源,或者如果您不需要指定对话特定资源,则只需将对话的资源设置为主窗口的资源:(
因为它是一个对话,所以我设置了
Owner属性也是如此)
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:
(Since it's a dialogue i set the
Owner
property as well)