Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.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插件,在这个插件中,我使用了一个主题/样式来更改所有按钮的最小宽度,如下所示: <Style TargetType="Button"> <Setter Property="MinWidth" Value="80" /> </Style> 在最新版本的现成产品中,他们自己从winforms迁移到WPF。现在,当我的插件加载时,以前影响我的插件表单的样式现在影响应用程序中的所有按钮。这使得大多数UI无法使用 我知道我可

我为一些现成产品编写了一个WPF插件,在这个插件中,我使用了一个主题/样式来更改所有按钮的最小宽度,如下所示:

<Style TargetType="Button">
    <Setter Property="MinWidth" Value="80" />
</Style>

在最新版本的现成产品中,他们自己从winforms迁移到WPF。现在,当我的插件加载时,以前影响我的插件表单的样式现在影响应用程序中的所有按钮。这使得大多数UI无法使用

我知道我可以使用基于字典键的资源将此样式特定于我的按钮,但这意味着我必须手动更改插件中的每个按钮,并且不要忘记在将来设置每个按钮的样式(以及此问题适用的其他元素)。还有其他选项可以使样式特定于一组按钮,如中所示,但我正在寻找一种方法,让我的样式只影响我的插件的样式(因此比参考问题中提到的要粗糙一些)。该插件由多个窗口/视图组成(与Caliburn.Micro绑定在一起)


是否有一种方法可以轻松地将样式范围限定为例如程序集或命名空间?我只想定义一次我的资源。目前,它是在Application.Resources级别定义的,如果还有更合适的,我也想听听。

您需要为样式指定一个键,并将样式应用于所有按钮

<Style TargetType="Button" x:Key="MyButtonStyle">
    <Setter Property="MinWidth" Value="80" />
</Style>


<Button Style="{StaticResource MyButtonStyle}"/>


如果没有键,样式将用于应用程序中的所有按钮(正如您已经注意到的)。

使用
ResourceDictionary
,我们可以设置默认样式,该样式将在Xaml中应用而不定义样式

DictionayNew.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:Media="clr-namespace:System.Windows.Media;assembly=PresentationCore"
                xmlns:System="clr-namespace:System;assembly=mscorlib">

    <!-- default button -->
    <Style TargetType="{x:Type Button}">
        <Setter Property="FontWeight" Value="Normal" />
        <Setter Property="VerticalAlignment" Value="Center" />
        <Setter Property="MinWidth" Value="80" />
    </Style>

    <!-- new button style --> 
    <Style x:Key="ActionButton" TargetType="{x:Type Button}">
        <Setter Property="FontWeight" Value="Normal" />
        <Setter Property="VerticalAlignment" Value="Center" />
        <Setter Property="MinWidth" Value="75" />
        <Setter Property="Height" Value="23" />
    </Style>

    <!-- new button style based on previous style -->
    <Style x:Key="BigActionButton"
       BasedOn="{StaticResource ActionButton}"
       TargetType="{x:Type Button}">
        <Setter Property="MinWidth" Value="150" />
        <Setter Property="Height" Value="30" />
    </Style>
</ResourceDictionary>
没有定义样式时,按钮将在字典中定义默认样式

编辑: 您可以按如下代码设置合并字典:

ResourceDictionary myResourceDictionary = new ResourceDictionary();
myResourceDictionary.Source = new Uri("DictionayNew.xaml", UriKind.Relative);
Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);

@Sinatr的可能重复我更新了我的问题,以强调为什么它与您引用的问题不同,但感谢链接,我以前从未见过它。我自己发现了这一点,我实际上在问题中谈到了这种类型的解决方案,以及它如何不适合我的问题。除了使用键之外,还有什么方法可以定义样式吗?我实际上没有定义自己的窗口,我使用Caliburn.Micro的IWindowManager来加载几个用户控件中的一个作为主要内容。我正在寻找一种方法来定义一次按钮样式。您的回答可以帮助我避免在每个按钮上定义样式,但我仍然需要在每个UserControl的ResourceDictionary中定义样式。你知道将样式应用于整个程序集或名称空间的方法吗?@dvdvvorle请参阅我的编辑,我添加了一种通过代码将样式应用于合并词典的方法
<Button Content="Refresh" />
<Button Content="Delete selected" Style="{DynamicResource ActionButton}" />
ResourceDictionary myResourceDictionary = new ResourceDictionary();
myResourceDictionary.Source = new Uri("DictionayNew.xaml", UriKind.Relative);
Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);