【问题标题】:How to store an ItemsSource for an ItemsControl (ListBox, ComboBox) in the app.config?如何在 app.config 中存储 ItemsControl(ListBox、ComboBox)的 ItemsSource?
【发布时间】:2011-01-29 21:37:21
【问题描述】:

经过大量研究和反复试验,我发现了如何在 app.Config 中存储 ListBox 和 ComboBox 的项目。我非常感谢 Jon Rista,他写了a series of articles about the Net 2.0 Configuration classes。我构建了一个(大!)代码 sn-p,只需插入三个字符串即可生成您需要的所有代码!

享受吧!

【问题讨论】:

    标签: .net configuration combobox app-config itemssource


    【解决方案1】:
    <?xml version="1.0" encoding="utf-8"?>
    <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
      <CodeSnippet Format="1.0.0">
        <Header>
          <Title>ConfigurationElementCollectionBasicMap</Title>
          <Author>Andre Steffens, heavily indebted to Jon Rista</Author>
          <Description>Derive from ConfigurationElementCollection of type BasicMap: Collection and Section</Description>
          <HelpUrl></HelpUrl>
          <SnippetTypes />
          <Keywords />
          <Shortcut>cecb</Shortcut>
        </Header>
        <Snippet>
          <References />
          <Imports />
          <Declarations>
            <Literal Editable="true">
              <ID>tag</ID>
              <Type></Type>
              <ToolTip></ToolTip>
              <Default>tag</Default>
              <Function></Function>
            </Literal>
            <Literal Editable="true">
              <ID>NameSpace</ID>
              <Type></Type>
              <ToolTip></ToolTip>
              <Default>NameSpace</Default>
              <Function></Function>
            </Literal>
            <Literal Editable="true">
              <ID>Type</ID>
              <Type></Type>
              <ToolTip></ToolTip>
              <Default>My</Default>
              <Function></Function>
            </Literal>
          </Declarations>
          <Code Language="csharp" Kind="type decl" Delimiter="$"><![CDATA[using System.Configuration;
    
    namespace $NameSpace$.Configuration
    {
        public class $Type$ElementCollection: ConfigurationElementCollection
        {
            #region Constructor
            public $Type$ElementCollection()
            {
            }
            #endregion
    
            #region Properties
            public override ConfigurationElementCollectionType CollectionType
            {
                get { return ConfigurationElementCollectionType.BasicMap; }
            }
            protected override string ElementName
            {
                get { return "$tag$"; }
            }
    
            protected override ConfigurationPropertyCollection Properties
            {
                get { return new ConfigurationPropertyCollection(); }
            }
            #endregion
    
            #region Indexers
            public $Type$Element this[int index]
            {
                get{ return ($Type$Element)BaseGet(index);}
    
                set
                {
                    if (BaseGet(index) != null) BaseRemoveAt(index);
                    base.BaseAdd(index, value);
                }
            }
    
            public new $Type$Element this[string key]
            {
                get { return ($Type$Element) BaseGet(key); }
            }
            #endregion
    
            #region Methods
            public void Add($Type$Element item)
            {
                base.BaseAdd(item);
            }
    
            public void Remove($Type$Element item)
            {
                BaseRemove(item);
            }
    
            public void RemoveAt(int index)
            {
                BaseRemoveAt(index);
            }
            #endregion
    
            #region Overrides
            protected override ConfigurationElement CreateNewElement()
            {
                return new $Type$Element();
            }
    
            protected override object GetElementKey(ConfigurationElement element)
            {
                var configurationElement = element as $Type$Element;
                return configurationElement == null
                           ? null
                           : configurationElement.Key;
            }
            #endregion
        }
    
        public class $Type$sSection: ConfigurationSection
        {
            #region Constructors
            static $Type$sSection()
            {
                s_name = new ConfigurationProperty(
                    "name",
                    typeof(string),
                    null,
                    ConfigurationPropertyOptions.IsRequired
                    );
    
                s_$tag$s = new ConfigurationProperty(
                    "",
                    typeof($Type$ElementCollection),
                    null,
                    ConfigurationPropertyOptions.IsRequired | ConfigurationPropertyOptions.IsDefaultCollection
                    );
    
                s_properties = new ConfigurationPropertyCollection {s_name, s_$tag$s};
            }
            #endregion
    
            #region Fields
            private static readonly ConfigurationPropertyCollection s_properties;
            private static readonly ConfigurationProperty s_name;
            private static readonly ConfigurationProperty s_$tag$s;
            #endregion
    
            #region Properties
            public string Name
            {
                get { return (string)base[s_name]; }
                set { base[s_name] = value; }
            }
    
            public $Type$ElementCollection $Type$s
            {
                get { return ($Type$ElementCollection)base[s_$tag$s]; }
            }
    
            protected override ConfigurationPropertyCollection Properties
            {
                get { return s_properties; }
            }
            #endregion
        }
    
        public class $Type$Element: ConfigurationElement
        {
            #region Constructors
            static $Type$Element()
            {
                s_key = new ConfigurationProperty(
                    "key",
                    typeof(string),
                    null,
                    ConfigurationPropertyOptions.IsRequired
                    );
    
                s_value = new ConfigurationProperty(
                    "value",
                    typeof(string),
                    null,
                    ConfigurationPropertyOptions.None
                    );
    
                //initialize further fields here
    
                s_properties = new ConfigurationPropertyCollection {s_key, s_value};
            }
            #endregion
    
            #region Fields
            private static readonly ConfigurationPropertyCollection s_properties;
            private static readonly ConfigurationProperty s_key;
            private static readonly ConfigurationProperty s_value;
            //add fields at your liberty!
    
            #endregion
    
            #region Properties
            public string Key
            {
                get { return (string)base[s_key]; }
                set { base[s_key] = value; }
            }
    
            public string Value
            {
                get { return (string)base[s_value]; }
                set { base[s_value] = value; }
            }
    
            //implement further properties here
    
            protected override ConfigurationPropertyCollection Properties
            {
                get { return s_properties; }
            }
        }
            #endregion
    }
    
    //example app.config snippet
    /*
    <configuration>
      <configSections>
        <section name="$tag$Listing" type="$NameSpace$.Configuration.$Type$sSection, $NameSpace$" />
      </configSections>
    
      <$tag$Listing name="$tag$s">
        <$tag$ key="key1" value="value1" />
        <$tag$ key="key2" value="value2" />
      </$tag$Listing>
    </configuration>
    */]]></Code>
        </Snippet>
      </CodeSnippet>
    </CodeSnippets>
    

    典型用法是:

    //WPF
    public Window1()
        {
            InitializeComponent();
            var section = ConfigurationManager.GetSection("$tag$Listing") as $Type$sSection;
            MyComboBox.ItemsSource = section.$Type$s;
            MyComboBox.DisplayMemberPath="Value";
        }
    
    //WinForms
    public Window1()
        {
            var section = ConfigurationManager.GetSection("$tag$Listing") as $Type$sSection;
            MyComboBox.Items = section.$Type$s;
            MyComboBox.DisplayMember="Value";
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-08
      • 2018-11-08
      • 2019-08-22
      • 2016-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多