NOTE: DO NOT TRY TO COPY FROM HERE, USE THIS MSDN SAMPLES PAGE INSTEAD, AS THIS WON'T COPY/PASTE WELL
Table of Contents
Array of String
<
x:Array
x:Key
=
"stringArray"
Type
=
"sys:String"
xmlns:sys
=
"clr-namespace:System;assembly=mscorlib"
>
<
sys:String
>Bear</
sys:String
>
<
sys:String
>Bird</
sys:String
>
<
sys:String
>Cat</
sys:String
>
<
sys:String
>Cow</
sys:String
>
<
sys:String
>Dog</
sys:String
>
<
sys:String
>Elephant</
sys:String
>
<
sys:String
>Fish</
sys:String
>
<
sys:String
>Goat</
sys:String
>
<
sys:String
>Hamster</
sys:String
>
<
sys:String
>Horse</
sys:String
>
<
sys:String
>Insect</
sys:String
>
<
sys:String
>Sheep</
sys:String
>
<
sys:String
>Snake</
sys:String
>
<
sys:String
>Monkey</
sys:String
>
</
x:Array
>
Array of Int
<
x:Array
x:Key
=
"intArray"
Type
=
"sys:Int32"
xmlns:sys
=
"clr-namespace:System;assembly=mscorlib"
>
<
sys:Int32
>0</
sys:Int32
>
<
sys:Int32
>1</
sys:Int32
>
<
sys:Int32
>2</
sys:Int32
>
<
sys:Int32
>3</
sys:Int32
>
<
sys:Int32
>4</
sys:Int32
>
<
sys:Int32
>5</
sys:Int32
>
<
sys:Int32
>6</
sys:Int32
>
<
sys:Int32
>7</
sys:Int32
>
<
sys:Int32
>8</
sys:Int32
>
<
sys:Int32
>9</
sys:Int32
>
</
x:Array
>
Array of Person
<
x:Array
x:Key
=
"personArray"
Type
=
"local:Person"
xmlns:sys
=
"clr-namespace:System;assembly=mscorlib"
xmlns:local
=
"clr-namespace:StaticDummyData.PocoModel"
>
<
local:Person
>
<
local:Person.FirstName
>Jack</
local:Person.FirstName
>
<
local:Person.LastName
>Jones</
local:Person.LastName
>
<
local:Person.Age
>42</
local:Person.Age
>
<
local:Person.JoinDate
>12/10/11</
local:Person.JoinDate
>
</
local:Person
>
<
local:Person
>
<
local:Person.FirstName
>Jill</
local:Person.FirstName
>
<
local:Person.LastName
>Johnson</
local:Person.LastName
>
<
local:Person.Age
>31</
local:Person.Age
>
<
local:Person.JoinDate
>10/09/08</
local:Person.JoinDate
>
</
local:Person
>
<
local:Person
>
<
local:Person.FirstName
>Tom</
local:Person.FirstName
>
<
local:Person.LastName
>Jones</
local:Person.LastName
>
<
local:Person.Age
>26</
local:Person.Age
>
<
local:Person.JoinDate
>08/02/12</
local:Person.JoinDate
>
</
local:Person
>
</
x:Array
>
I use two types of Person class. A POCO class for basic demos and XAML static data, and a INotifyPropertyChanged implemented version for binding examples.
A POCO Person class
using
System;
namespace
StaticDummyData.PocoModel
{
public
class
Person
{
public
string
FirstName {
get
;
set
; }
public
string
LastName {
get
;
set
; }
public
int
Age {
get
;
set
; }
public
DateTime JoinDate {
get
;
set
; }
}
}
An INotifyPropertyChanged implemented Person class
using
System;
using
System.ComponentModel;
namespace
StaticDummyData.MvvmModel
{
public
class
Person : INotifyPropertyChanged
{
string
_FirstName;
public
string
FirstName
{
get
{
return
_FirstName;
}
set
{
if
(_FirstName != value)
{
_FirstName = value;
RaisePropertyChanged(
"FirstName"
);
}
}
}
string
_LastName;
public
string
LastName
{
get
{
return
_LastName;
}
set
{
if
(_LastName != value)
{
_LastName = value;
RaisePropertyChanged(
"LastName"
);
}
}
}
int
_Age;
public
int
Age
{
get
{
return
_Age;
}
set
{
if
(_Age != value)
{
_Age = value;
RaisePropertyChanged(
"Age"
);
}
}
}
DateTime _JoinDate;
public
DateTime JoinDate
{
get
{
return
_JoinDate;
}
set
{
if
(_JoinDate != value)
{
_JoinDate = value;
RaisePropertyChanged(
"JoinDate"
);
}
}
}
void
RaisePropertyChanged(
string
prop)
{
if
(PropertyChanged !=
null
) { PropertyChanged(
this
,
new
PropertyChangedEventArgs(prop)); }
}
public
event
PropertyChangedEventHandler PropertyChanged;
}
}
XAML Usage Examples
The x:Array markup code above is placed in the Resources tag. If it is a durable project, place them in App.xaml (Application.Resources) and use as shown below:
<
Window
x:Class
=
"StaticDummyData.MainWindow"
Title
=
"MainWindow"
Height
=
"350"
Width
=
"525"
>
<
StackPanel
>
<
ComboBox
ItemsSource
=
"{StaticResource stringArray}"
/>
<
ComboBox
ItemsSource
=
"{StaticResource intArray}"
/>
<
DataGrid
ItemsSource
=
"{StaticResource personArray}"
/>
</
StackPanel
>
</
Window
>
If it is a quick demo, you can dump this on the same Page/Window/UserControl that uses it:
<
Window
x:Class
=
"StaticDummyData.MainWindow"
Title
=
"MainWindow"
Height
=
"350"
Width
=
"525"
>
<
Window.Resources
>
<
x:Array
x:Key
=
"stringArray"
Type
=
"sys:String"
xmlns:sys
=
"clr-namespace:System;assembly=mscorlib"
>
<
sys:String
>Bear</
sys:String
>
<
sys:String
>Bird</
sys:String
>
<
sys:String
>Cat</
sys:String
>
</
x:Array
>
</
Window.Resources
>
<
StackPanel
>
<
ComboBox
ItemsSource
=
"{StaticResource stringArray}"
/>
</
StackPanel
>
</
Window
>
You can even embed it in the actual control that consumes it, but then it has to be a DynamicReference:
<
ComboBox
ItemsSource
=
"{DynamicResource stringArray}"
>
<
ComboBox.Resources
>
<
x:Array
x:Key
=
"stringArray"
Type
=
"sys:String"
xmlns:sys
=
"clr-namespace:System;assembly=mscorlib"
>
<
sys:String
>Bear</
sys:String
>
<
sys:String
>Bird</
sys:String
>
<
sys:String
>Cat</
sys:String
>
</
x:Array
>
</
ComboBox.Resources
>
</
ComboBox
>
These are all snippets I use regularly in my community work and in prototyping designs. I use this page as a quick reference for that purpose.
NOTE: DO NOT TRY TO COPY FROM HERE, USE THIS MSDN SAMPLES PAGE INSTEAD, AS THIS WON'T COPY/PASTE WELL