2017-02-17 66 views
-1

我试图根据我的要求覆盖Material Design for Xaml ToolKit的样式,以下是我在阅读关于覆盖github页面上的关于重写的文章后提出的app.xaml中的xaml图书馆,但它似乎没有工作,我没有得到为什么,因为我在WPF应用程序的工作没有太多的经验,这里是我试过的代码:使用资源字典覆盖特定样式属性

<Color x:Key="DarkBlueColor">#00479D</Color> 
<FontFamily x:Key="MicrosoftYaHei">Microsoft YaHei</FontFamily> 
<SolidColorBrush x:Key="WindowBrush" Color="#00479D"/> 

<Style x:Key="WindowStyle" 
     x:Name="WindowStyle" 
     BasedOn="{StaticResource MaterialDesignPaper}" 
     TargetType="{x:Type Window}"> 
    <Setter Property="Background" Value="{DynamicResource WindowBrush}"></Setter> 
</Style> 

对于作为时间来熟悉我很只是试图改变窗口的背景,这里是从MainWindow.xaml代码:

<Window x:Class="WPFApplication.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:local="clr-namespace:XCMG.CarMan2" 
     mc:Ignorable="d" 
     Title="MainWindow" Height="350" Width="525" 
     Style="{StaticResource WindowStyle}" 
     xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"> 
<Grid> 

</Grid> 
</Window> 

当运行添加上述代码之后该应用程序,它抛出异常说:

无法转换类型的对象“System.Windows.Media.SolidColorBrush”为类型“系统。 Windows.Style”。

回答

1

“MaterialDesignPaper”是SolidColorBrush,你不能在Brush基地Window风格。

取出BasedOn属性和x:NameStyle

<Style x:Key="WindowStyle" 
     TargetType="{x:Type Window}"> 
    <Setter Property="Background" Value="{DynamicResource WindowBrush}"></Setter> 
</Style> 

,但我想重写MaterialDesignBrush背景

定义一个新的Brush资源使用相同的密钥,那么:

<SolidColorBrush x:Key="MaterialDesignPaper" Color="#00479D"/> 
+0

但我想覆盖MaterialDesignBrush BackGround –

+0

然后用相同的键定义一个新的画笔资源。看到我编辑的答案。 – mm8

+0

它是否会覆盖现有的风格的颜色属性与此键在另一个DLL? –