在xaml中创建一个字典? [英] Create a Dictionary in xaml?

查看:130
本文介绍了在xaml中创建一个字典?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伪示例:

<Window>
  <Window.Tag>
    <x:Dictionary KeyType="{x:Type sys:String}" ValueType="{x:Type sys:Int32}">
        <sys:DictionaryEntry Entry="{sys:DictionaryEntry Key0, 000}"/>
        <sys:DictionaryEntry Key="key1" Value="111"/>
        <sys:DictionaryEntry>
          <sys:DictionaryEntry.Key>
            <sys:String>Key2<sys:String>
          </sys:DictionaryEntry.Key>          
          <sys:DictionaryEntry.Value>
            <sys:Int32>222</sys:Int32>            
          </sys:DictionaryEntry.Value>
        </sys:DictionaryEntry>
    </x:Dictionary />    
  </Window.Tag>
</Window>


推荐答案

您不能使用直接在XAML中的字典< TKey,TValue> 类,因为没有办法指定通用类型的参数(在下一个版本的XAML中可能是可以的,但VS2010中不支持WPF设计师...至少不在初始版本中)

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).

但是,您可以声明一个继承自 Dictionary< TKey,TValue> ,并在XAML中使用。

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>

这篇关于在xaml中创建一个字典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆