Csharp/C#教程:将属性添加到自定义WPF控件?分享


将属性添加到自定义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; 

需要改为:

上述就是C#学习教程:将属性添加到自定义WPF控件?分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

 public static DependencyProperty GradientStartProperty; public static DependencyProperty GradientEndProperty; 

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/1017083.html

(0)
上一篇 2021年12月31日
下一篇 2021年12月31日

精彩推荐