WPF绑定:属性与变量

时间:2011-11-18 14:53:18

标签: c# wpf binding

我已将公共属性转换为公共变量,以使代码更简单,现在组合框的绑定机制将无效。我不能使用变量而不是属性吗?

工作代码:属性

    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
        }
    }

1 个答案:

答案 0 :(得分:6)

这称为字段 您无法将数据绑定到字段。

相反,您可以将该字段设为私有,并创建一个返回该字段的公共属性。您仍然可以使用字段初始值设定项。