WPF如何将样式应用于自定义控件而不覆盖派生样式

问题描述 投票:0回答:1

我正在安装MaterialDesign的WPF自定义控件。我的自定义控件包含一个使用派生/默认material design button的按钮。将样式应用于此按钮时,材质设计样式为overridden,以支持默认的wpf按钮。如何在保留基础材料设计样式的同时应用样式?

Generic.xaml -CustomButton

 <Style TargetType="{x:Type local:CustomButton}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:CustomButton}">
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">
                        <Button Name="MaterialButton"
                                Content="CustomButton">
                            <Button.Style>
                                <Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type  Button}}">
                                    <Style.Triggers>
                                        <Trigger Property="IsMouseOver" Value="True">
                                            <Setter Property="Foreground" Value="Red"/>
                                        </Trigger>
                                    </Style.Triggers>
                                </Style>
                            </Button.Style>
                        </Button>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

App.xaml-(材料设计在资源字典中。)

 <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>

我尝试过的事情

我已经尝试按照建议的BasedOnhere使用here属性,但这无法解决我的问题。

源自基于"Material Design theme is lost"的材料样式常见问题解答,导致此错误:例外:找不到名为“ MaterialDesignFlatButton”的资源。资源名称区分大小写。

Generic.Xaml尝试对CustomButton进行更改

<!--Same custon control as before--->
<Button.Style>
    <Style TargetType="{x:Type Button}" BasedOn="{StaticResource MaterialDesignFlatButton}">
        <!--same trigger-->
wpf xaml material-design wpf-controls material-design-in-xaml
1个回答
1
投票

看来您尚未安装正确的依赖项。右键单击使用样式的项目,然后选择NuGet程序包管理器。搜索MaterialDesignThemes程序包并进行安装。如果已经安装,请重新安装。这应该可以解决。

您的其余方法是正确的。

或者从GitHub存储库中复制original style(快照)和所有引用,并将其粘贴到您的App.xaml

如果已安装所有依赖项(或复制了原始样式,则使用以下Style将MaterialDesign默认Button样式应用于您的CustomButton

<Style TargetType="local:CustomButton" 
       BasedOn="{StaticResource MaterialDesignFlatButton}" />

更新

现在您已经编辑了问题以提供必要的详细信息,我可以告诉您出了什么问题。

您不仅简单地扩展了Button,还覆盖了扩展后的Style的默认Button。为此,需要在Generic.xaml文件中定义默认的Style。由于解析此默认资源的方式,此文件不允许合并字典。仅允许引用Generic.xaml的相同ResourceDictionary中的资源。

要解决此问题,您需要显式覆盖默认的Style。覆盖Style必须在MaterialDesignFlatButton资源的范围内,否则引用将无法解析,并导致您之前发布的错误消息。以下示例在App.xaml中定义替代Style

  <Style TargetType="CustomButton" 
         BasedOn="{StaticResource MaterialDesignFlatButton}">
    <Setter Property="OverridesDefaultStyle" Value="True" />
  </Style>
</ResourceDictionary>

备注

由于您对默认Style不感兴趣,因此应详细了解此默认Style,例如ControlTemplate至优先级Style。然后,您可以从Generic.xaml中删除默认的Style。然后,您还需要从样式化自定义控件的静态构造函数中删除以下行,例如CustomButton

DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomButton), new FrameworkPropertyMetadata(typeof(CustomButton)));
© www.soinside.com 2019 - 2024. All rights reserved.