WPF 数据模板

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)
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值