将属性添加到自定义WPF控件?

Son*_*Boy 11 .net c# wpf xaml

我今天早上刚刚开始WPF,所以这是(希望)一个容易解决的问题.我开始创建一个具有渐变背景的按钮.我想在控件的属性中声明渐变开始和结束颜色,然后将它们应用到模板中.我在编译代码时遇到了麻烦.我得到的例外是xaml告诉我属性不可访问但是当我将visiblity修饰符改为public时它抱怨它无法找到静态属性...

到目前为止,这是我的xaml:

<StackPanel>
    <StackPanel.Resources>
        <Style TargetType="my:GradientButton">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type my:GradientButton}">
                        <Grid>
                            <Ellipse Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" Stroke="{TemplateBinding Foreground}" VerticalAlignment="Top" HorizontalAlignment="Left">
                                <Ellipse.Fill>
                                    <LinearGradientBrush>
                                        <GradientStop Color="{TemplateBinding GradientStart}" Offset="0"></GradientStop><!--Problem on this line!!!-->
                                        <GradientStop Color="{TemplateBinding GradientEnd}" Offset="1"></GradientStop>
                                    </LinearGradientBrush>
                                </Ellipse.Fill>
                            </Ellipse>
                            <Polygon Points="18,12 18,38, 35,25" Fill="{TemplateBinding Foreground}" />
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </StackPanel.Resources>
    <my:GradientButton x:Name="btnPlay" Height="50" Width="50" Foreground="Black" Click="Button_Click" GradientStart="#CCCCCC" GradientEnd="#7777777" />
</StackPanel>
Run Code Online (Sandbox Code Playgroud)

这是我的自定义控件的代码:

public class GradientButton : Button
{
    static DependencyProperty GradientStartProperty;
    static DependencyProperty GradientEndProperty;

    static GradientButton()
    {
        GradientStartProperty = DependencyProperty.Register("GradientStart", typeof(Color), typeof(GradientButton));
        GradientEndProperty = DependencyProperty.Register("GradientEnd", typeof(Color), typeof(GradientButton));
    }

    public Color GradientStart
    {
        get { return (Color)base.GetValue(GradientStartProperty); }
        set { base.SetValue(GradientStartProperty, value); }
    }

    public Color GradientEnd
    {
        get { return (Color)base.GetValue(GradientEndProperty); }
        set { base.SetValue(GradientEndProperty, value); }
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑:这是我得到的设计时例外

Cannot reference the static member 'GradientStartProperty' on the type 'GradientButton' as it is not accessible.
Run Code Online (Sandbox Code Playgroud)

Son*_*Boy 11

我想通了......这个:

static DependencyProperty GradientStartProperty; 
static DependencyProperty GradientEndProperty;
Run Code Online (Sandbox Code Playgroud)

需要改为:

public static DependencyProperty GradientStartProperty; 
public static DependencyProperty GradientEndProperty;
Run Code Online (Sandbox Code Playgroud)