我是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)
Zah*_*tar 30
而不是在依赖项属性元数据中插入null
new UIPropertyMetadata("YOUR DEFAULT VALUE GOES HERE")
Run Code Online (Sandbox Code Playgroud)
所以现在变成了
public static readonly DependencyProperty MyFruitProperty =
DependencyProperty.Register("MyFruit", typeof(Fruit), typeof(CircularGauge), new UIPropertyMetadata("YOUR DEFAULT VALUE GOES HERE"));
Run Code Online (Sandbox Code Playgroud)
小智 5
您需要像这样使用属性元数据
class MyValidation
{
public bool status
{
get { return (bool)GetValue(statusProperty); }
set { SetValue(statusProperty, value); }
}
public static readonly DependencyProperty statusProperty =
DependencyProperty.Register("status", typeof(bool), typeof(MyValidation),new PropertyMetadata(false));
Run Code Online (Sandbox Code Playgroud)
}
| 归档时间: |
|
| 查看次数: |
19961 次 |
| 最近记录: |