小编Dae*_*ire的帖子

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

我正在基于通用 Windows 搜索栏 (我的版本)在 wpf 中构建自定义控件搜索框。在 Windows 实现中,按钮将根据是否按下按钮来改变背景和前景。为此,我使用样式和触发器来更改按钮颜色。

当我使用 MaterialDesign 时,我的自定义控件包含一个使用派生/默认材料设计按钮的按钮。当样式应用于此按钮时,材料设计样式将被覆盖,以支持默认的 wpf 按钮。如何在保持基础材料设计风格的同时应用风格?

由于我的问题只是将样式应用于按钮,因此我省略了搜索框样式,而是创建了一个仅包含按钮的问题示例。

Generic.xaml-自定义按钮

 <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}">
                        <!-- Normally the below button would be inside a grid with additional buttons, labels and content presenter. I have removed these for simplicity-->
                        <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> …
Run Code Online (Sandbox Code Playgroud)

wpf xaml wpf-controls material-design material-design-in-xaml

4
推荐指数
1
解决办法
3469
查看次数