WPF绑定:属性vs变量

本文关键字:vs 变量 属性 绑定 WPF | 更新日期: 2023-09-27 18:15:01

我已经将一个公共属性转换为公共变量,以使代码更简单,现在组合框的绑定机制将无法工作。我不能用变量代替属性吗?

工作代码:As property

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

WPF绑定:属性vs变量

这叫做字段
不能对字段进行数据绑定。

相反,您可以将该字段设置为私有,并设置返回该字段的公共属性。您仍然可以使用字段初始化器