将属性添加到自定义WPF控件?
我今天早上刚开始参加WPF,所以这是(希望)一个容易解决的问题。 我开始创建一个具有渐变背景的按钮。 我想在控件的属性中声明渐变开始和结束颜色,然后将它们应用于模板中。 我在编译代码时遇到了麻烦。 我得到的例外是xaml告诉我属性不可访问但是当我将visiblity修饰符改为public时它会抱怨它无法找到静态属性…
到目前为止这是我的xaml:
这是我的自定义控件的代码:
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); } } }
编辑:这是我得到的设计时例外
Cannot reference the static member 'GradientStartProperty' on the type 'GradientButton' as it is not accessible.
我想通了……这个:
static DependencyProperty GradientStartProperty; static DependencyProperty GradientEndProperty;
需要改为:
public static DependencyProperty GradientStartProperty; public static DependencyProperty GradientEndProperty;