2012-10-25 68 views
0

在我再次运行下面的代码之前,如何知道“Status”DependencyProperty是否已注册?检查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()); 
+0

http://social.msdn.microsoft.com/Forums/en/wpf/thread/cdd0dcdf-9187-4cd9-80de-4d3f51a4f970 – Klaus78

回答

0

你需要反思,找出那些已经注册的Depenedency属性。

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"); 
     } 
    } 
} 
+0

我得到的非静态字段需要一个目标。错误在if(fInfo.GetValue(null)==“状态”) –

+0

您是否为cwindow创建了一个实例?是一个control.If如此用实例名称替换foreach中的cWindow。因为我不知道您的实例名称i使用类型名称cWindow.use .GetType()。GetFields() –

+0

我正在改变CWindow为“this”。我认为是目前的窗口。 –