How to get a List collection of values from app.config in WPF?

前端 未结 7 1134
执笔经年
执笔经年 2020-11-28 22:42

The following example fills the ItemsControl with a List of BackupDirectories which I get from code.

How can I change this

7条回答
  •  忘掉有多难
    2020-11-28 23:32

    I love Richard Nienaber's answer, but as Chuu pointed out, it really doesn't tell how to accomplish what Richard is refering to as a solution. Therefore I have chosen to provide you with the way I ended up doing this, ending with the result Richard is talking about.

    The solution

    In this case I'm creating a greeting widget that needs to know which options it has to greet in. This may be an over-engineered solution to OPs question as I am also creating an container for possible future widgets.

    First I set up my collection to handle the different greetings

    public class GreetingWidgetCollection : System.Configuration.ConfigurationElementCollection
    {
        public List All { get { return this.Cast().ToList(); } }
    
        public GreetingElement this[int index]
        {
            get
            {
                return base.BaseGet(index) as GreetingElement;
            }
            set
            {
                if (base.BaseGet(index) != null)
                {
                    base.BaseRemoveAt(index);
                }
                this.BaseAdd(index, value);
            }
        }
    
        protected override ConfigurationElement CreateNewElement()
        {
            return new GreetingElement();
        }
    
        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((GreetingElement)element).Greeting;
        }
    }
    

    Then we create the acutal greeting element and it's interface

    (You can omit the interface, that's just the way I'm always doing it.)

    public interface IGreeting
    {
        string Greeting { get; set; }
    }
    
    public class GreetingElement : System.Configuration.ConfigurationElement, IGreeting
    {
        [ConfigurationProperty("greeting", IsRequired = true)]
        public string Greeting
        {
            get { return (string)this["greeting"]; }
            set { this["greeting"] = value; }
        }
    }
    

    The greetingWidget property so our config understands the collection

    We define our collection GreetingWidgetCollection as the ConfigurationProperty greetingWidget so that we can use "greetingWidget" as our container in the resulting XML.

    public class Widgets : System.Configuration.ConfigurationSection
    {
        public static Widgets Widget => ConfigurationManager.GetSection("widgets") as Widgets;
    
        [ConfigurationProperty("greetingWidget", IsRequired = true)]
        public GreetingWidgetCollection GreetingWidget
        {
            get { return (GreetingWidgetCollection) this["greetingWidget"]; }
            set { this["greetingWidget"] = value; }
        }
    }
    

    The resulting XML

    
    
       
           
               
               
               
               ...
               
               
           
        
    
    

    And you would call it like this

    List greetings = Widgets.GreetingWidget.All;
    

提交回复
热议问题