2011-11-18 54 views
0

我已經將公共屬性轉換爲公共變量,以使代碼更簡單,現在組合框的綁定機制將不起作用。我不能使用變量而不是屬性?WPF綁定:屬性與變量

工作代碼:作爲財產

internal class Utility 
    { 
     #region ReportOf 
     public enum ReportOf 
     { 
      Choose, All, Group, Person 
     } 

     private static Dictionary<ReportOf, string> _dictReportOf; 
     public static Dictionary<ReportOf, string> ReportOfCollection 
     { 
      get { return _dictReportOf; } 
     } 
     #endregion ReportOf 


     static Utility() 
     { 
      //initialize the collection with user friendly strings for each enum 
      _dictReportOf = new Dictionary<ReportOf, string>(){ 
       {ReportOf.Choose, "Lütfen seçiniz..."},   
       {ReportOf.All, "Herkes"}, 
       {ReportOf.Group, "Grup"}, 
       {ReportOf.Person, "Şahıs"}}; 

     } 
    } 

非工作端口:可變

internal class Utility 
    { 
     #region ReportOf 
     public enum ReportOf 
     { 
      Choose, 
      All, 
      Group, 
      Person 
     } 

     public static Dictionary<ReportOf, string> ReportOfCollection = new Dictionary<ReportOf, string>() 
     { 
       {ReportOf.Choose, "Lütfen seçiniz..."},   
       {ReportOf.All, "Herkes"}, 
       {ReportOf.Group, "Grup"},     
       {ReportOf.Person, "Şahıs"} 
     }; 
     #endregion ReportOf 

     static Utility() 
     { 
      //Nothing to do 
     } 
    } 

回答

6

這就是所謂的一個
您無法將數據綁定到某個字段。

相反,您可以將該字段設置爲私有,並創建一個返回它的公共屬性。您仍然可以使用字段初始值設定項。

+1

事實上,你通常不想這樣做,因爲你需要綁定到屬性,讓它們擁有觸發有用事物的setter,如INotifyPropertyChanged。 –