WPF: Specify Combo Box ItemsSource from XAML

Use of x:Array in XAML
To define itemsource from XAML using x:Array, we need to add the following namespaces.
xmlns:sys="clr-namespace:System;assembly=mscorlib"

We can create an array of string, or any other simple data type in Resources section.
We have created an array of string in the following example:
<x:Array x:Key="StringArrayResource" Type="sys:String">
<sys:String>Combo Value1</sys:String>
<sys:String>Combo Value2</sys:String>
<sys:String>Combo Value3</sys:String>
</x:Array>


In the above example we can see that Type is specified as sys:String where 'sys' is the System NameSpace from mscorlib assembly.
Note we have given key "StringArrayResource" for the string array.
Now the main part, Binding Combo Box ItemsSource with the resource defined.
<ComboBox x:Name="comboBox" ItemsSource="{StaticResource StringArrayResource}" Height="30" Width="90">          
</ComboBox>


The Combo Box will look like this.


No comments:

Post a Comment