21

疑似例:

<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>
4

4 に答える 4

32

ジェネリック型引数を指定する方法がないため、クラスを XAML で直接使用することはできませんDictionary<TKey, TValue>(XAML の次のバージョンでは可能になりますが、VS2010 WPF デザイナーではサポートされません...少なくとも最初のリリースで)。

ただし、 から継承する非ジェネリック クラスを宣言しDictionary<TKey, TValue>て、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>
于 2010-02-23T16:14:22.767 に答える
7

キーと値が文字列の場合は、ListDictionary または HybridDictionary を使用できます。

例えば:

<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>
于 2013-06-24T14:49:58.717 に答える
5

関連する質問で、代わりにカスタムマークアップ拡張機能を使用して、XAML 2009 機能を使用せずに XAML でジェネリック ディクショナリを作成する方法を示す回答を提供しまし

于 2012-02-26T02:44:48.600 に答える
4

次のようなことを試してください:

この名前空間を使用します: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>
于 2016-06-30T21:22:54.187 に答える