Silverlight: 在XMAL使用自定類別
當我們在開發Silverlight應用程式時,除了能在程式中自行建立類別,也能在XMAL上自定類別和程式互相呼應,這樣能夠更直覺在XMAL上調用
這裡就介紹一下如何在XMAL自定類別
在Silverlight專案建立新的類別Person.cs及People.cs
建立一建構子及Person屬性
namespace CustomTypes
{
public class Person
{
public Person()
{
}
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
}
}
{
public class Person
{
public Person()
{
}
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
}
}
People是Person的集合
using System.Collections.Generic;
namespace CustomTypes
{
public class People
{
public People()
{
ThePeople = new List<Person>();
}
public List<Person> ThePeople { get; set; }
}
}
namespace CustomTypes
{
public class People
{
public People()
{
ThePeople = new List<Person>();
}
public List<Person> ThePeople { get; set; }
}
}
xmlns:local="clr-namespace:CustomTypes 將這一段加入至UserControl 才能夠使用自定類別
使用<UserControl.Resources>方式將資料放置在XMAL上,並且將ListBox元件與資料繫結
<UserControl x:Class="CustomTypes.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:CustomTypes"
Width="400" Height="300">
<UserControl.Resources>
<local:People x:Key="PeopleList">
<local:People.ThePeople>
<local:Person
FirstName="Tom"
LastName="Lee"
Age="28"/>
<local:Person
FirstName="John"
LastName="Wu"
Age="20"/>
<local:Person
FirstName="Jack"
LastName="Chen"
Age="18"/>
</local:People.ThePeople>
</local:People>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White">
<ListBox
DataContext="{StaticResource PeopleList}"
ItemsSource="{Binding ThePeople}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" >
<TextBlock Text="{Binding FirstName}" Margin="5,0,0,0"/>
<TextBlock Text="{Binding LastName}" Margin="5,0,0,0"/>
<TextBlock Text="{Binding Age}" Margin="5,0,0,0"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</UserControl>
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:CustomTypes"
Width="400" Height="300">
<UserControl.Resources>
<local:People x:Key="PeopleList">
<local:People.ThePeople>
<local:Person
FirstName="Tom"
LastName="Lee"
Age="28"/>
<local:Person
FirstName="John"
LastName="Wu"
Age="20"/>
<local:Person
FirstName="Jack"
LastName="Chen"
Age="18"/>
</local:People.ThePeople>
</local:People>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White">
<ListBox
DataContext="{StaticResource PeopleList}"
ItemsSource="{Binding ThePeople}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" >
<TextBlock Text="{Binding FirstName}" Margin="5,0,0,0"/>
<TextBlock Text="{Binding LastName}" Margin="5,0,0,0"/>
<TextBlock Text="{Binding Age}" Margin="5,0,0,0"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</UserControl>
那如果要在程式內讀取XAML資料怎麼辦?
People people = this.Resources["PeopleList"] as People;
這樣就能讀取到物件了