小编Mic*_*its的帖子

如何为从dependencyobject派生的类型的依赖项属性设置默认值

我是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)

c# wpf xaml

21
推荐指数
2
解决办法
2万
查看次数

标签 统计

c# ×1

wpf ×1

xaml ×1