【发布时间】:2011-07-18 07:02:23
【问题描述】:
我是 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
【问题讨论】: