【发布时间】:2020-06-09 16:30:30
【问题描述】:
我正在 wpf 中基于 common windows search bar (my version) 构建一个自定义控件搜索框。在 Windows 实现中,按钮将根据是否按下它们来改变背景和前景。为此,我使用样式和触发器来更改按钮颜色。
当我使用 MaterialDesign 时,我的自定义控件包含一个使用派生/默认 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}">
<!-- 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>
</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>
我的尝试
我已尝试按照建议的here 和here 使用BasedOn 属性,但这并没有解决我的问题。
源自基于"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