2010-02-23 12 views
20

ejemplo Pseudo:¿Crear un diccionario en 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> 
+0

Más arriba-a-fecha de discusión en https://stackoverflow.com/ preguntas/2494823/binding-dictionaryt-to-a-wpf-listbox – Ben

Respuesta

27

Usted no puede utilizar la clase Dictionary<TKey, TValue> directamente en XAML, porque no hay forma de especificar los argumentos de tipo genérico (que será posible en la próxima versión de XAML, pero ganó' t ser compatible con el diseñador VS2010 WPF ... al menos no en la versión inicial).

Sin embargo, puede declarar una clase no genérica que hereda de Dictionary<TKey, TValue> y usarla en 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

¿Qué se entiende por * será posible en la próxima versión de XAML * ¿tiene alguna idea de cuándo planean implementarlo? – Shimmy

+0

@Shimmy: en realidad, ya está implementado en XAML 2009. Desafortunadamente, VS2010 aún no es compatible con XAML 2009 :( –

+2

Para más detalles, consulte este video: http://channel9.msdn.com/pdc2008/TL36/ (XAML 2009) nuevas características se presentan a partir de 7'20) –

5

En un related question i dio una answer que demuestra cómo se puede crear un diccionario genérico en XAML sin los XAML 2009 funciones usando una costumbre Markup Extension en su lugar.

+0

Lo revisé. Sí ¡es genial! – Shimmy

+0

@Shimmy: Gracias :) –

6

Si las claves y los valores son cadenas, puede utilizar ListDictionary o HybridDictionary.

Por ejemplo:

<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> 
+1

Declaración del espacio de nombres: 'xmlns: Specialized =" clr-namespace: System.Collections.Specialized; assembly = System "' – Pollitzer

4

intentar algo como esto:

uso este espacio de nombres: 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

Nueva respuesta a una publicación anterior, pero exactamente lo que necesitaba. – Pat

Cuestiones relacionadas