XAML中绑定自定义字典类

5 篇文章 0 订阅
2 篇文章 0 订阅
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>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

赵庆明老师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值