简体   繁体   English

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

[英]Adding Properties to Custom WPF Control?

I just started WPF this morning so this is (hopefully) an easy question to solve. 我今天早上刚刚开始WPF,所以这是(希望)一个容易解决的问题。 I've started with creating a button that has a gradient background. 我开始创建一个具有渐变背景的按钮。 I want to declare the gradient start and end colors in the property of the control and then apply them in the template. 我想在控件的属性中声明渐变开始和结束颜色,然后将它们应用到模板中。 I'm having trouble getting the code to compile though. 我在编译代码时遇到了麻烦。 The exception I'm getting is that the xaml is telling me the property is not accessible but when I chang the visiblity modifier over to public it complains it can't find the static property... 我得到的例外是xaml告诉我属性不可访问但是当我将visiblity修饰符改为public时它抱怨它无法找到静态属性...

Here's my xaml so far: 到目前为止,这是我的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>

And here's the code for my custom control: 这是我的自定义控件的代码:

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); }
    }
}

EDIT: Here's the design-time exception I'm getting 编辑:这是我得到的设计时例外

Cannot reference the static member 'GradientStartProperty' on the type 'GradientButton' as it is not accessible.

I figured it out... This: 我想通了......这个:

static DependencyProperty GradientStartProperty; 
static DependencyProperty GradientEndProperty;

Needed to be changed to this: 需要改为:

public static DependencyProperty GradientStartProperty; 
public static DependencyProperty GradientEndProperty;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM