【问题标题】:How to override ComboBox ToggleButton from Material Design for XAML?如何从 Material Design for XAML 覆盖 ComboBox ToggleButton?
【发布时间】:2021-03-14 05:52:42
【问题描述】:

我是 WPF 的新手。 我对覆盖 MaterialDesignComboBoxToggleButton 有疑问 风格。我想用自己的替换“模板”设置器,但我的控制模板中的内容总是被忽略。为什么会这样?使用其他样式我没有这个问题。

下面的代码演示了我需要什么。

Overriedes.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.ComboBox.xaml" />
</ResourceDictionary.MergedDictionaries>

<Style BasedOn="{StaticResource MaterialDesignComboBoxToggleButton}" TargetType="{x:Type ToggleButton}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ToggleButton}">
                <TextBlock FontSize="50" FontWeight="Bold">$$</TextBlock>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

App.xaml

<Application x:Class="Wpf.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         
         xmlns:local="clr-Wpf" xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
         StartupUri="Views/MainWindow.xaml">
<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <materialDesign:CustomColorTheme BaseTheme="Light" PrimaryColor="#FFD8E1FF" SecondaryColor="#FFD8E1FF" />
            <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
            <ResourceDictionary Source="/Overrides.xaml" />
        </ResourceDictionary.MergedDictionaries>
        
    </ResourceDictionary>
</Application.Resources>

【问题讨论】:

    标签: c# wpf xaml material-design-in-xaml


    【解决方案1】:

    将相同的资源键添加到您的自定义样式中,如下所示:

    <Style 
        x:Key="MaterialDesignComboBoxToggleButton"
        BasedOn="{StaticResource MaterialDesignComboBoxToggleButton}" 
        TargetType="{x:Type ToggleButton}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ToggleButton}">
                    <TextBlock FontSize="50" FontWeight="Bold">$$</TextBlock>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    

    并且你不需要在Overrides.xaml文件中添加这样的资源:

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

    因为您在App.xaml 文件中添加的MaterialDesignTheme.Defaults.xaml 已经包含了MDIX 资源。

    【讨论】:

    • 我已经尝试过这种情况,但不幸的是,它不起作用。我的 sn-ps 遵循了类似问题override-style-not-working 的答案,这种风格的这种情况也不起作用。不知道实际发生了什么。感谢您的回答
    • 同意,这是可行的,但对于一种情况使用:&lt;ToggleButton Style="{StaticResource MaterialDesignComboBoxToggleButton}" /&gt;。但是组合框中的切换按钮没有样式引用呢?
    • 似乎 MaterialDesignComboBoxToggleButton 这样的依赖样式在将资源字典与此样式的覆盖合并之前解析,因此我的更改未应用。因此需要覆盖整个 MaterialDesignCombobox 样式?如果是这样,这将迫使我下载源代码并对其进行必要的更改:)
    猜你喜欢
    • 2021-04-23
    • 2015-10-05
    • 1970-01-01
    • 2010-12-20
    • 1970-01-01
    • 1970-01-01
    • 2015-09-12
    • 1970-01-01
    • 2018-12-24
    相关资源
    最近更新 更多