XAML中綁定自定義字典類

xmlns:local="clr-namespace:WpfApp20"
 <Window.Resources>
         <x:Array x:Key="data" Type="local:MyDictionary">
            <local:MyDictionary>
                <local:MyDictionary.Key>Mykey</local:MyDictionary.Key>
                <local:MyDictionary.Value>MyValye</local:MyDictionary.Value>
            </local:MyDictionary>
            <local:MyDictionary>
                <local:MyDictionary.Key>Mykey1</local:MyDictionary.Key>
                <local:MyDictionary.Value>MyValye2</local:MyDictionary.Value>
            </local:MyDictionary>
        </x:Array>
 </Window.Resources>
  public class MyDictionary
    {
        public string Key { set; get; }
        public string Value { set; get; }
    }
<ListBox
            x:Name="listBox"
            DisplayMemberPath="Key"
            ItemsSource="{Binding Mode=OneWay, Source={StaticResource data}}"
            SelectedValuePath="Value" />

可以直接使用DictionaryEntry,這樣就不需要定義“MyDictionary”類了。
xmlns:dict=“clr-namespace:System.Collections;assembly=mscorlib”

	<x:Array x:Key="data" Type="dict:DictionaryEntry">
            <dict:DictionaryEntry>
                <dict:DictionaryEntry.Key>Mykey</dict:DictionaryEntry.Key>
                <dict:DictionaryEntry.Value>MyValye</dict:DictionaryEntry.Value>
            </dict:DictionaryEntry>
            <dict:DictionaryEntry>
                <dict:DictionaryEntry.Key>Mykey1</dict:DictionaryEntry.Key>
                <dict:DictionaryEntry.Value>MyValye2</dict:DictionaryEntry.Value>
            </dict:DictionaryEntry>
        </x:Array>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章