自定义控件:如何覆盖默认属性?

时间:2018-08-14 08:15:58

标签: c# wpf properties custom-controls

我创建了一个类似于按钮的自定义控件。 我想覆盖他的默认属性,但是效果很奇怪。

UserControl Xaml

<UserControl x:Class="project.MyButton"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             mc:Ignorable="d" 
             d:DesignHeight="50" d:DesignWidth="50">
    <Grid>
        <Ellipse x:Name="structure" Fill="Black" Stroke="White" StrokeThickness="2"/>
        <Label x:Name="content" Content="&#xE081;" VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="White" FontFamily="Segoe MDL2 Assets" FontWeight="Bold" FontSize="16"/>
    </Grid>
</UserControl>

背后的用户控制代码(C#)

    /// <summary>A simple custom button.</summary>
    public partial class MyButton : UserControl
    {
        /// <summary>Background color.</summary>
        public new Brush Background { get => structure.Fill; set => structure.Fill = value; }

        /// <summary>Foreground color.</summary>
        public new Brush Foreground { get => content.Foreground; set => content.Foreground = value; }

        /// <summary>Stroke color.</summary>
        public Brush Stroke { get => structure.Stroke; set => structure.Stroke = value; }

        /// <summary>Stroke thickness.</summary>
        public double StrokeThickness { get => structure.StrokeThickness; set => structure.StrokeThickness = value; }

        /// <summary>Font family.</summary>
        public new FontFamily FontFamily { get => content.FontFamily; set => content.FontFamily = value; }

        /// <summary>Font weight.</summary>
        public new FontWeight FontWeight { get => content.FontWeight; set => content.FontWeight = value; }

        /// <summary>Font size.</summary>
        public new double FontSize { get => content.FontSize; set => content.FontSize = value; }

        /// <summary>Content.</summary>
        public new object Content { get => content.Content; set => content.Content = value; }


        /// <summary>Inizialize new <see cref="MyButton"/>.</summary>
        public MyButton()
        {
            InitializeComponent();
        }
    }

结果

Custom round button

但是,当我尝试设置Background属性时,它会为控件而不是椭圆形的背景上色,当我尝试设置Content时,它将覆盖按钮等。

示例:Background="Red"

Background Red

2 个答案:

答案 0 :(得分:1)

您可以为ControlTemplate定义一个UserControl

<UserControl x:Class="project.MyButton"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:WpfApp4"
             mc:Ignorable="d" 
             d:DesignHeight="50" d:DesignWidth="50">
    <UserControl.Template>
        <ControlTemplate TargetType="UserControl">
            <Grid>
                <Ellipse x:Name="structure" Fill="{TemplateBinding Background}" Stroke="White" StrokeThickness="2"/>
                <Label x:Name="content" Content="{TemplateBinding Content}" VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="White" FontFamily="Segoe MDL2 Assets" FontWeight="Bold" FontSize="16"/>
            </Grid>
        </ControlTemplate>
    </UserControl.Template>
</UserControl>

使用TemplateBinding作为模板中en元素的属性,您可以设置UserControl的此属性来更改模板中元素的属性:

<local:Button Background="Red" Content="&#xE081;" />

答案 1 :(得分:0)

UserControl具有BackroundContent属性,它们是DependencyProperties的包装。

您正尝试在属性之前使用new关键字对它们进行阴影处理,但操作失败。 (在setter中设置一个断点:永不打断)

从xaml设置Background="smth"Content="smth"时,将检测到基本属性,此外,WPF不使用包装器属性,而是直接使用DependencyObject.SetValue()。设置基本Content只需使用Ellipse删除现有的Grid。

重命名背景-> CustomBackground,内容-> CustomContent并进行设置。然后MyButton将起作用。

更好的方法是重用基本的Content和Background(将它们绑定到MyButton xaml中的Ellipse和Lable)。