我是WPF的新手,这是我的第一篇文章.我创建了一个名为'Fruit'的类,它来自'DependencyObject',并添加了名为'Apple'的额外属性.我创建了一个新的自定义控件,其中包含一个名为'MyFruit'的类型为'Fruit'的依赖属性.我的问题是,如何在'MyFruit'对象中设置属性的默认值(即'Apple'属性?我想在XAML中使用该对象设置它.
public class Gauge : Control
{
.
.
.
//---------------------------------------------------------------------
#region MyFruit Dependency Property
public Fruit MyFruit
{
get { return (Fruit)GetValue(MyFruitProperty); }
set { SetValue(MyFruitProperty, value); }
}
public static readonly DependencyProperty MyFruitProperty =
DependencyProperty.Register("MyFruit", typeof(Fruit), typeof(CircularGauge), null);
#endregion
}
//-------------------------------------------------------------------------
#region Fruit class
public class Fruit : DependencyObject
{
private int apple;
public int Apple
{
get { return apple; }
set { apple = value; }
}
}
#endregion
Run Code Online (Sandbox Code Playgroud)