Nime Cloud
Nime Cloud

Reputation: 6395

WPF Binding: Property vs variable

I've converted a public property as public variable to make code simpler and now a combobox's binding mechanism won't work. Can't I use variable instead of property?

Working code: 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"}};

        }
    }

Non-working port: As variable

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

Upvotes: 0

Views: 294

Answers (1)

SLaks
SLaks

Reputation: 887489

That's called a field.
You cannot data-bind to a field.

Instead, you can make the field private and make a public property that returns it. You'll still be able to use the field initializer.

Upvotes: 6

Related Questions