WPF 数据模板
示例一:
x:Class="WPF0418A.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<DataTemplate x:Key="btnDataTemplate">
<StackPanel>
<TextBlock Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Button}, Path=FontFamily}"></TextBlock>
<TextBlock Text="hello world"></TextBlock>
</StackPanel>
</DataTemplate>
</Window.Resources>
<Grid>
<StackPanel>
<Button ContentTemplate="{StaticResource btnDataTemplate}"/>
</StackPanel>
</Grid>
</Window>
运行效果:
示例二:
窗体XAML代码:
<Window x:Class="WPF0418A.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<DataTemplate x:Key="btnDataTemplate">
<StackPanel>
<TextBlock Margin="5" Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Button}, Path=DataContext.Name}"></TextBlock>
<TextBlock Margin="5" Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Button}, Path=DataContext.Number}"></TextBlock>
</StackPanel>
</DataTemplate>
</Window.Resources>
<Grid>
<StackPanel>
<Button Name="button1" ContentTemplate="{StaticResource btnDataTemplate}"/>
</StackPanel>
</Grid>
</Window>
窗体后台代码:
namespace WPF0418A
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
objStu = new Student() { Name="Danny",Number=1004};
button1.DataContext = ObjStu;
}
private Student objStu;
public Student ObjStu
{
get { return objStu; }
set { objStu = value; }
}
}
public class Student
{
public string Name { get; set; }
public int Number { get; set; }
}
}
运行效果:(该按钮使用内容模板,然后内容模板绑定了一个类元素的两个属性值)
示例三:列表控件数据模板
运行效果:
窗体后台代码:
namespace WPF0418C
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
ListStudent.Add(new Student() { Name = "张三", Number = 10001 });
ListStudent.Add(new Student() { Name = "李四", Number = 10002 });
ListStudent.Add(new Student() { Name = "王五", Number = 10003 });
this.DataContext = this;
}
private List<Student> listStudent = new List<Student>();
public List<Student> ListStudent
{
get { return listStudent; }
set { listStudent = value; }
}
}
public class Student
{
public string Name { get; set; }
public int Number { get; set; }
}
}
窗体XAML代码:
<Window x:Class="WPF0418C.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<DataTemplate x:Key="listboxDataTemplate">
<StackPanel Orientation="Horizontal">
<TextBlock Foreground="Green" Margin="5" Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBoxItem}, Path=DataContext.Name}"></TextBlock>
<TextBlock Foreground="Red" Margin="5" Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBoxItem}, Path=DataContext.Number}"></TextBlock>
</StackPanel>
</DataTemplate>
</Window.Resources>
<Grid>
<StackPanel>
<ListBox ItemsSource="{Binding ListStudent}" ItemTemplate="{StaticResource listboxDataTemplate}">
</ListBox>
</StackPanel>
</Grid>
</Window>
**示例四:**ItemPanelTemplate的使用
<Window x:Class="WPF0418D.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<ItemsPanelTemplate x:Key="itemPanelTemplate">
<StackPanel Orientation="Horizontal">
</StackPanel>
</ItemsPanelTemplate>
</Window.Resources>
<Grid>
<StackPanel>
<ListBox Margin="5">
<ListBoxItem>星期一</ListBoxItem>
<ListBoxItem>星期二</ListBoxItem>
<ListBoxItem>星期三</ListBoxItem>
<ListBoxItem>星期四</ListBoxItem>
<ListBoxItem>星期五</ListBoxItem>
</ListBox>
<ListBox Margin="5" ItemsPanel="{StaticResource itemPanelTemplate}">
<ListBoxItem>星期一</ListBoxItem>
<ListBoxItem>星期二</ListBoxItem>
<ListBoxItem>星期三</ListBoxItem>
<ListBoxItem>星期四</ListBoxItem>
<ListBoxItem>星期五</ListBoxItem>
</ListBox>
</StackPanel>
</Grid>
</Window>
效果:(其中第二个ListBox就是使用了ItemsPanelTemplate)