【问题标题】:Can't apply WPF theme on the window itself无法在窗口本身上应用 WPF 主题
【发布时间】:2012-07-26 10:09:45
【问题描述】:

我正在尝试应用 Windows Presentation Foundation (WPF) CodePlex 项目中的 ExpressionDark 主题。

我已将文件 ExpressionDark.xaml 添加到 Themes 子文件夹中,并更改了我的 App.xaml 文件以引用此文件:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary
                Source="Themes/ExpressionDark.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

当我运行程序时,样式会应用于所有控件,但不会应用于窗口本身。窗口的背景保持白色。我的理解是该样式适用于Window 类,但不适用于派生类。

为了解决这个问题,我尝试在我的app.xaml 文件中创建一个新样式,以便为我的自定义窗口类继承 Windows 样式:

<Application.Resources>
    <ResourceDictionary>
        <Style
            TargetType="{x:Type local:MainWindow}"
            BasedOn="{StaticResource {x:Type Window}}" />
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary
                Source="Themes/ExpressionDark.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

结果完全相同。

我也尝试了WPF: Adventures in Theming (Part 2) 的建议。我添加到我的窗口定义中:

<Window
    x:Class="MyProject.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:vm="clr-namespace:MyProject.ViewModel"
    Title="MainWindow"
    Height="350"
    Width="525"
    Style="{DynamicResource {x:Type Window}}" />

没有更多的成功。

缺少什么?

PS:不知道是否重要,但我的窗口的孩子是一个网格。

【问题讨论】:

    标签: wpf themes


    【解决方案1】:

    在 Window XAML 中使用动态资源指定背景

    <Window x:Class="HellowWorld.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="Hellow World" Height="614" Width="821"
            Background="{DynamicResource WindowBackgroundBrush}">
    

    【讨论】:

      【解决方案2】:

      上次我检查主题中没有Window 样式。

      【讨论】:

      • 在这种情况下,您有什么建议要正确设置窗口的背景颜色吗?
      • @SteveB: 主题中应该有定义的资源(我认为是ControlBackgroundBrush),直接在Background属性中引用即可(或者样式的Setter)。跨度>
      • &lt;Setter Property="Background" Value="{StaticResource ShadeBrush}" /&gt;... 明白了!只是想知道为什么主题页面上的屏幕截图定义了背景颜色(没有我可以探索的示例)。
      • @SteveB:(你确定要使用ShadeBrush吗?)我认为没有样式是因为缺少继承。
      • 不,我不确定。我是使用主题的新手,我希望通过引用它来动态应用开箱即用的样式。我发现这还不够。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-05
      • 1970-01-01
      相关资源
      最近更新 更多