Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 替换所有控件的默认WPF主题样式_C#_Wpf_Xaml - Fatal编程技术网

C# 替换所有控件的默认WPF主题样式

C# 替换所有控件的默认WPF主题样式,c#,wpf,xaml,C#,Wpf,Xaml,我尝试在WPF项目中为所有控件替换默认样式。 我创建了一个ResourceDictionary,如下所示: <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Style x:Key="{x:Type

我尝试在WPF项目中为所有控件替换默认样式。 我创建了一个
ResourceDictionary
,如下所示:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style x:Key="{x:Type Button}" TargetType="{x:Type Button}">
        <Setter Property="Background" Value="Black"/>
    </Style>
</ResourceDictionary>
现在,在我的
main window.Xaml中,我创建了一个按钮,样式就在上面。
我想包装一个新按钮
MyButton
谁继承了button

我希望每个
按钮
定义的样式在我将其放在
主窗口.Xaml上时都会显示在
MyButton
上,但这不会发生

还有一件重要的事情我不想使用
BasedOn


我在一些博客上看到了这段代码:

<Application>
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source=”{ThemeDictionary MyApplication}”/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

但它不起作用


谢谢

请为密钥指定一个字符串名称:

<Style x:Key="MyButton" TargetType="{x:Type Button}">
    <Setter Property="Background" Value="Black"/>
</Style>

并将其添加到App.XAML(应用程序.Resources块外部):


还有一件很重要的事我不想用BasedOn为什么不呢?我在一些博客上看到了这段代码:但它不起作用!!!
<Style x:Key="MyButton" TargetType="{x:Type Button}">
    <Setter Property="Background" Value="Black"/>
</Style>
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource MyButton}" />