2012-10-25 71 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」。我認爲是目前的窗口。 –