【问题标题】:how to set style to window with ResourceDictionary如何使用 ResourceDictionary 将样式设置为窗口
【发布时间】:2014-08-26 09:03:38
【问题描述】:

我创建了一个名为 Dictionary1.xaml 的 ResourceDictionary,代码如下:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="{x:Type Window}">
    <Setter Property="Background" Value="Red">
    </Setter>
</Style>

我在 App.xaml 或 MainWindow.xaml 中这样引用它:

<Application.Resources>
    <ResourceDictionary Source="Dictionary1.xaml" />
</Application.Resources>

 <Window.Resources>
    <ResourceDictionary Source="Dictionary1.xaml" />
 </Window.Resources>

在视图设计器中,窗口的背景变为红色,但是当应用程序运行时,它的背景是默认的(白色),为什么?怎么办?

【问题讨论】:

    标签: c# wpf xaml


    【解决方案1】:

    你可以通过这种方式设置其他控件的样式,Window除外!尝试为Button,Lable等设置sytle,你会得到正确的结果。但是对于一个窗口,你不会。

    看我的另一个答案,它可能对你有帮助:How to add a common control on all my Windows?

    您必须设置样式的x:Key 并明确设置窗口的样式:

    Style="{DynamicResource key_name}"

    为什么视图设计器显示正确的结果?这可能是一个错误。 vs2012/13的xaml Designer有很多bug,可以在msdn中搜索或者commit。我已经commit了一个:

    https://connect.microsoft.com/VisualStudio/feedbackdetail/view/925324/multibinding-report-an-issue-on-latest-vs-xaml-editor

    但是ms关闭了,他们最近不会修复它。

    【讨论】:

      猜你喜欢
      • 2010-11-16
      • 2012-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-26
      • 1970-01-01
      • 2017-12-14
      相关资源
      最近更新 更多