检查DependencyProperty已注册

时间:2012-10-25 05:19:54

标签: c# wpf

在我再次运行下面的代码之前,如何知道是否已注册“Status”DependencyProperty?

代码:

public readonly DependencyProperty StatusProperty ;

    public string Status
    {
        get { return (string)GetValue(StatusProperty); }
        set { SetValue(StatusProperty, value); }
    }

StatusProperty = DependencyProperty.Register("Status", typeof(string), typeof(CWindow), new PropertyMetadata());

1 个答案:

答案 0 :(得分:0)

您需要反思才能找到已注册的依赖项属性。

using System.Reflection;

foreach (FieldInfo fInfo in CWindow.GetType().GetFields())
{
    if (fInfo.FieldType.Name == "DependencyProperty")
    {
        if (fInfo.GetValue(null) == "Status")
        {
            Console.WriteLine("Status Property already Registered");
        }
    }
}
相关问题