Silverlight: declaring a data collection in XAML?

I would like to declare some data in a Silverlight application for Windows Phone 7. I'm not sure what the syntax is.

For instance:

public class Person 
{
      public string Name {get; set;}
      public int Age {get; set;}
}

<Application.Resources>
    <Data x:Name="People">
         <Person Age="2" Name="Sam" />
         <!-- ... -->
    </Data>
</Application.Resources>

Obviously Datanot a valid tag. What do i need here?

+5
source share
1 answer

You will need to determine the type of container in the first place: -

using System.Collections.ObjectModel;

...

public class People : ObservableCollection<Person> { }

Then you need to add the namespace in which your People / Person classes are present in a line typical of Xaml that would look like this: -

<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:local="clr-namespace:SilverlightApplication1"
         x:Class="SilverlightApplication1.App"
         >

Just replace "SilverlightApplication1" with the application namespace.

Now you can: -

     <Application.Resources>
         <People x:Name="People">
             <Person Age="2" Name="Sam" />
             <Person Age="11" Name="Jane" />
         </People>
     </Application.Resources>
+6
source

All Articles