如何为公共基类创建默认样式

时间:2016-11-26 04:41:03

标签: c# wpf xaml custom-controls

目的

我有一个针对ToggleButton的自定义控件,该控件也适用于Button。所以我想为这两种类型使用通用的默认ControlTemplate

我尝试的策略是在模板中设置TargetType="{x:Type ButtonBase}",如果在ButtonToggleButton上明确设置,则此工作正常。

自定义控件库

在项目根目录的Themes文件夹中名为Generic.xaml的资源字典中......

<Style TargetType="{x:Type ButtonBase}">
    <Setter Property="Template">
        <Setter.Value>
            <!--Modified Control Template-->
            <ControlTemplate TargetType="{x:Type ButtonBase}">

在控件的类中,我使用FrameworkElement.DefaultStyleKey在其静态构造函数中设置用户控件类型的元数据...

static ContentToggle()
{
    DefaultStyleKeyProperty.OverrideMetadata(typeof(ContentToggle),
       new FrameworkPropertyMetadata(typeof(ButtonBase)));
}

使用WPF应用程序项目

...的App.xaml

<Application x:Class="Spec.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>

            <!--Get a reference to the window to establish View Context-->
            <RelativeSource x:Key="View" Mode="FindAncestor" 
                        AncestorType="{x:Type Window}" />

            <ResourceDictionary.MergedDictionaries>

                <!--Local Style-->
                <ResourceDictionary Source="pack://application:,,,/ButtonStyle.xaml" />

            </ResourceDictionary.MergedDictionaries>

        </ResourceDictionary>
    </Application.Resources>
</Application>

... MainWindow.xaml

<Window x:Class="Spec.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:b="clr-namespace:ContentToggleButton;assembly=ContentToggleButton"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <b:ContentToggle Name="Toggle" Height="30" 
                         Content="{Binding options, RelativeSource={StaticResource View}}"
                         />
    </Grid>
</Window>

MainWindow.xaml.cs

public partial class MainWindow : Window
{
    public List<string> options { get; set; }
    public bool initialState { get; set; }

    public MainWindow ()
    {
        options = new List<string> { "Checked", "UnChecked" };

        initialState = false;

        InitializeComponent();
    }
}

还有一个名为ButtonStyle.xaml的文件,它定义了自定义控件使用的画笔。它通过App.xaml中的合并字典在应用程序的根目录中公开。

结果

ContentToggle instance的模板为null,样式控件没有视觉效果(当我窥探控件时,它没有子元素。)

我的理解是自动ButtonBase样式/模板将用于我的控制。我错过了什么?

如果在控件上显式声明了样式/模板,则自定义控件将按预期工作。以下方法适用于将样式目标设置为ButtonBase ...

使用WPF应用程序项目

在App.xaml中......

<Application x:Class="Spec.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>

            <!--Get a reference to the window to establish View Context-->
            <RelativeSource x:Key="View" Mode="FindAncestor" 
                        AncestorType="{x:Type Window}" />

            <ResourceDictionary.MergedDictionaries>

                <!--custom control-->
                <ResourceDictionary Source="pack://application:,,,/ContentToggleButton;component/Themes/Generic.xaml" />

                <!--Local Style-->
                <ResourceDictionary Source="pack://application:,,,/ButtonStyle.xaml" />

            </ResourceDictionary.MergedDictionaries>

        </ResourceDictionary>
    </Application.Resources>
</Application>

在MainWindow.xaml ...

<Window x:Class="Spec.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:b="clr-namespace:ContentToggleButton;assembly=ContentToggleButton"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <b:ContentToggle Name="Toggle" Height="30" 
                         Content="{Binding options, RelativeSource={StaticResource View}}"
                         Style="{DynamicResource LocalButtonStyle}"
                         />
    </Grid>
</Window>

在ButtonStyle.xaml ...

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <!--Custom Button backgrounds-->
    <LinearGradientBrush x:Key="Button.Static.Background" 
                             EndPoint="0.5,1" StartPoint="0.5,0">
        <GradientStop Color="#FF2C0606" Offset="1"/>
        <GradientStop Color="#E6ADAD"/>
    </LinearGradientBrush>
    <LinearGradientBrush x:Key="Button.MouseOver.Background" EndPoint="0.5,1" StartPoint="0.5,0">
        <GradientStop Color="#FF2C0606" Offset="1"/>
        <GradientStop  Color="#FFF2F2"/>
    </LinearGradientBrush>
    <LinearGradientBrush x:Key="Button.MouseOver.Checked.Background" EndPoint="0.5,1" StartPoint="0.5,0">
        <GradientStop Color="#FF2C0606" Offset="1"/>
        <GradientStop  Color="#F2FFF3"/>
    </LinearGradientBrush>
    <LinearGradientBrush x:Key="Button.Checked.Background" EndPoint="0.5,1" StartPoint="0.5,0">
        <GradientStop Color="#FF2C0606" Offset="1"/>
        <GradientStop x:Name="GradientStop" Color="#ADE6B1"/>
    </LinearGradientBrush>

    <!--Establish the style colours-->
    <SolidColorBrush x:Key="Button.Static.Border" Color="#FF707070" />
    <SolidColorBrush x:Key="Button.MouseOver.Border" Color="#FF3C7FB1" />
    <SolidColorBrush x:Key="Button.Pressed.Background" Color="#C4F6CE" />
    <SolidColorBrush x:Key="Button.Pressed.Border" Color="#FF2C628B" />
    <SolidColorBrush x:Key="Button.Disabled.Background" Color="#FFF4F4F4" />
    <SolidColorBrush x:Key="Button.Disabled.Border" Color="#FFADB2B5" />
    <SolidColorBrush x:Key="Button.Disabled.Foreground" Color="#FF838383" />

    <!--Custom Style-->
    <Style x:Key="LocalButtonStyle" TargetType="{x:Type ButtonBase}" BasedOn="{StaticResource ButtonStyle}">
        <Setter Property="HorizontalAlignment" Value="Stretch" />
        <Setter Property="VerticalAlignment" Value="Center" />
        <Setter Property="HorizontalContentAlignment" Value="Center" />
        <Setter Property="VerticalContentAlignment" Value="Center" />
        <Setter Property="Background" 
                    Value="{StaticResource Button.Static.Background}"/>
        <Setter Property="BorderBrush" 
                    Value="{StaticResource Button.Static.Border}"/>
    </Style>

</ResourceDictionary>

解决方案结构(两种情况都相同)

enter image description here

2 个答案:

答案 0 :(得分:0)

我不知道为什么,但对于ButtonBase主题搜索根本不起作用(可能与它是抽象的相关)。即使您尝试使用ButtonBase或类似方法找到FindResource的样式 - 您也无法找到任何内容,即使您自己在Generic.xaml中定义它(对于其他控件,例如按钮你会发现一种风格)。

现在,为了解决您的特定问题,最简单的方法是为ContentTogger定义样式,但为ButtonBase定义模板(您可以将其移出单独的资源并重用):

<Style TargetType="{x:Type c:ContentToggle}">
    <Setter Property="Template">
        <Setter.Value>
            <!-- move this template out to separate resource -->
            <ControlTemplate TargetType="{x:Type ButtonBase}">
                <Border Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

答案 1 :(得分:0)

在开始阅读您的问题介绍后,我建议采用以下方法。这只是一个给出基本想法的示例。

<强> Generic.xaml

<Style x:Key="CommonStyle" TargetType="{x:Type Button}">
    <Setter Property="Background" Value="Yellow"/>
    <Setter Property="FontSize" Value="25"/>
</Style>

<Style TargetType="{x:Type local:CustomButton}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:CustomButton}">
                <Border Background="{TemplateBinding Background}" CornerRadius="10"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                    <Button Style="{StaticResource CommonStyle}" Content="{TemplateBinding Content}" />
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<强> CustomButton.cs

public class CustomButton : ToggleButton
{
    static CustomButton()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomButton), new FrameworkPropertyMetadata(typeof(CustomButton)));
    }           
}

看看这是否能解决您的问题,否则我会尝试改进这个答案。