如何从XAML的材料设计中覆盖组合框切换按钮?

svgewumm  于 2023-02-20  发布在  其他
关注(0)|答案(2)|浏览(127)

我是WPF的新手。我有一个重写MaterialDesignComboBoxToggleButton样式的问题。我想用自己的替换“模板”设置器,但是我的控件模板内容总是被忽略。为什么会发生这种情况?使用其他样式我没有这个问题。
贝娄代码演示了我所需要的。

覆盖.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>

应用程序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>
vdgimpew

vdgimpew1#

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

<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>

因为MDIX资源已包含在MaterialDesignTheme.Defaults.xaml中,您已将其添加到App.xaml文件中。

gg0vcinb

gg0vcinb2#

在我的例子中,窗口中的覆盖样式不起作用,但当我尝试在App.xml中使用时,它起作用了。我将切换按钮的样式更改为另一个样式。代码为:

<Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Themes/IG/IG.MSControls.Core.Implicit.xaml" />
                <ResourceDictionary Source="Themes/IG/IG.MSControls.Toolkit.Implicit.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.Blue.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Indigo.xaml" />

            </ResourceDictionary.MergedDictionaries>

            <Style TargetType="{x:Type ToggleButton}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="ToggleButton">
                            <Border CornerRadius="8" BorderBrush="{TemplateBinding BorderBrush}" 
                                Background="{TemplateBinding Background}">
                                <ContentPresenter HorizontalAlignment="Center"                  
                                              VerticalAlignment="Center"/>
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
                <Style.Triggers>
                    <Trigger Property="IsChecked" Value="True">
                        <Setter Property="Background" Value="#FF0F0F4B" />
                        <Setter Property="Foreground" Value="White" />
                    </Trigger>
                    <Trigger Property="IsChecked" Value="False">
                        <Setter Property="Background" Value="#FF73ADDE" />
                        <Setter Property="Foreground" Value="#FF150404" />
                    </Trigger>
                </Style.Triggers>
            </Style>



        </ResourceDictionary>
    </Application.Resources>

相关问题