Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a Dictionary in xaml?

Tags:

Pseudo example:

<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> 
like image 464
Shimmy Weitzhandler Avatar asked Feb 23 '10 15:02

Shimmy Weitzhandler


People also ask

What is a XAML resource dictionary?

A resource dictionary is a repository for XAML resources, such as styles, that your app uses. You define the resources in XAML and can then retrieve them in XAML using the {StaticResource} markup extension and {ThemeResource} markup extension s. You can also access resources with code, but that is less common.

How do I add resources to XAML dictionary?

Tip You can create a resource dictionary file in Microsoft Visual Studio by using the Add > New Item… > Resource Dictionary option from the Project menu. Here, you define a resource dictionary in a separate XAML file called Dictionary1.

What is ResourceDictionary WPF?

In Extensible Application Markup Language (XAML), the ResourceDictionary class is typically an implicit collection element that is the object element value of several Resources properties, when given in property element syntax. For details on implicit collections in XAML, see XAML Syntax Terminology.

What is resource Dictionary in xamarin forms?

A ResourceDictionary is a repository for resources that are used by a Xamarin. Forms application. Typical resources that are stored in a ResourceDictionary include styles, control templates, data templates, colors, and converters.


2 Answers

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

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> 
like image 132
Thomas Levesque Avatar answered Oct 11 '22 07:10

Thomas Levesque


If the keys and values are strings, you can use ListDictionary or HybridDictionary.

For example:

<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> 
like image 33
Alexzander Avatar answered Oct 11 '22 08:10

Alexzander