Create a Dictionary in xaml?

前端 未结 4 850
礼貌的吻别
礼貌的吻别 2020-12-03 07:07

Pseudo example:


  
    
        

        
相关标签:
4条回答
  • 2020-12-03 07:44

    Try something like this:

    use this namespace: xmlns:collections="clr-namespace:System.Collections;assembly=mscorlib"

    <ComboBox.ItemsSource>
        <collections:ArrayList>
            <collections:DictionaryEntry Key="0" Value="Standby"/>
            <collections:DictionaryEntry Key="1" Value="Maintenance"/>
            <collections:DictionaryEntry Key="2" Value="Available"/>
            <collections:DictionaryEntry Key="3" Value="Deselected"/>
            <collections:DictionaryEntry Key="4" Value="Input Error"/>
        </collections:ArrayList>
    </ComboBox.ItemsSource>
    
    0 讨论(0)
  • 2020-12-03 07:50

    In a related question i gave an answer which shows how one could create a generic dictionary in XAML without the XAML 2009 features using a custom Markup Extension instead.

    0 讨论(0)
  • 2020-12-03 08:09

    If the keys and values are strings, you can use ListDictionary or HybridDictionary.

    For example:

    <Specialized:ListDictionary x:Key="MasterSlidesFileNames">
        <System:String x:Key="long">Ya long yes ni</System:String>
        <System:String x:Key="Sun">Waterfall</System:String>
        <System:String x:Key="lorem ipsum">hello wOrld</System:String>
    </Specialized:ListDictionary>
    
    0 讨论(0)
  • 2020-12-03 08:10

    You can't use the Dictionary<TKey, TValue> class directly in XAML, because there's no way to specify the generic type arguments (it will be possible in the next version of XAML, but it won't be supported in VS2010 WPF designer... at least not in the initial release).

    However, you can declare a non-generic class that inherits from Dictionary<TKey, TValue>, and use it in XAML.

    C#

    public class MyDictionary : Dictionary<string, int> { }
    

    XAML

    <Window>
      <Window.Tag>
        <local:MyDictionary>
            <sys:Int32 x:Key="key0">0</sys:Int32>
            <sys:Int32 x:Key="key1">111</sys:Int32>
            <sys:Int32 x:Key="key2">222</sys:Int32>
        </local:MyDictionary />    
      </Window.Tag>
    </Window>
    
    0 讨论(0)
提交回复
热议问题