WPF的DataTemplate(数据模板)

一、概述

DataTemplate顾名思义,就是数据模板,用来指定数据的表现形式。这对于ItemsControl类的控件尤其有用,可以改变列表项的外观,更具有表现能力。

二、实例

View Code 

<Grid>
    <Grid.Resources>
        <src:Customers x:Key="customers"/>
    </Grid.Resources>

    <ListBox ItemsSource="{StaticResource customers}" Width="350" Margin="0,5,0,10">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Padding="5,0,5,0"
          Text="{Binding FirstName}" />
                    <TextBlock Text="{Binding LastName}" />
                    <TextBlock Text=", " />
                    <TextBlock Text="{Binding Address}" />
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>

上例中通过指定ListBox.ItemTemplate属性来定义子项的显示格式,如果不是列表项,可以通过控件的ContentTemplate属性来指定应用哪个数据模板。

使用起来比较简单,大概分为三步:

1.如果有些属性不是可以直接使用,需要转换的,写转换器,实现IValueConverter接口

2.定义好转换器之后,需要实例化,指定给需要的绑定

3.定义模板,也就是需要的显示格式

4.应用模板,把定义好的模板指定到目标控件的ContentTemplate或者ItemTemplate

看一个完整的例子

1.用来封装的实体类和转换器

View Code 

class Car
    {
        public string Automaker { get; set; }
        public string Name { get; set; }
        public string Year { get; set; }
        public string TopSpeed { get; set; }
    }

    //厂商名称转化为Logo图片
    public class AutomakerToLogoPathConverter:IValueConverter
    {
        
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string uriStr = string.Format(@"images/{0}.jpg",value.ToString());
            return new BitmapImage(new Uri(uriStr, UriKind.Relative));
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    //汽车名称转化为图像
    public class NameToPhotoPathConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string uriStr = string.Format(@"images/{0}.jpg", value.ToString());
            return new BitmapImage(new Uri(uriStr, UriKind.Relative));
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

2.模板及窗体

View Code 

<Window x:Class="TempaletPractise.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:TempaletPractise"
        Title="汽车展览" Height="350" Width="623">
    <Window.Resources>
        <!--Converter-->
        <local:AutomakerToLogoPathConverter x:Key="a2l"></local:AutomakerToLogoPathConverter>
        <local:NameToPhotoPathConverter x:Key="n2p"></local:NameToPhotoPathConverter>
        
        <!--Data Template for Detail View-->
        <DataTemplate x:Key="carDetailViewTemplate">
            <Border BorderBrush="Black" BorderThickness="1" CornerRadius="6">
                <StackPanel Margin="5">
                    <Image Width="400" Height="250" Source="{Binding Name, Converter={StaticResource n2p}}"></Image>
                    <StackPanel Orientation="Horizontal" Margin="5,0">
                        <TextBlock Text="Name:" FontWeight="Bold" FontSize="20"></TextBlock>
                        <TextBlock Text="{Binding Name}" FontSize="20" Margin="5,0"></TextBlock>
                    </StackPanel>
                    <StackPanel Orientation="Horizontal" Margin="5,0">
                        <TextBlock Text="Automaker" FontWeight="Bold"></TextBlock>
                        <TextBlock Text="{Binding Automaker}"  Margin="5,0"></TextBlock>
                        <TextBlock Text="Year" FontWeight="Bold"></TextBlock>
                        <TextBlock Text="{Binding Year}" Margin="5,0"></TextBlock>
                        <TextBlock Text="Top Speed" FontWeight="Bold"></TextBlock>
                        <TextBlock Text="{Binding TopSpeed}"  Margin="5,0"></TextBlock>
                    </StackPanel>
                </StackPanel>
            </Border>
        </DataTemplate>
        
        <!--DataTemplate for Item View-->
        <DataTemplate x:Key="carListItemViewTemplate">
            <Grid Margin="2">
                <StackPanel Orientation="Horizontal">
                    <Image Source="{Binding Automaker,Converter={StaticResource a2l}}" 
                           Grid.RowSpan="3" Width="64" Height="64"></Image>
                    <StackPanel Margin="5,0">
                        <TextBlock Text="{Binding Name}" FontSize="16" FontWeight="Bold"></TextBlock>
                        <TextBlock Text="{Binding Year}" FontSize="14"></TextBlock>
                    </StackPanel>
                </StackPanel>
            </Grid>
        </DataTemplate>
    </Window.Resources>
    <!--窗体的内容-->
    <StackPanel Orientation="Horizontal" Margin="5">
        <UserControl ContentTemplate="{StaticResource carDetailViewTemplate}"
                      Content="{Binding SelectedItem, ElementName=listBoxCars}"></UserControl>
        <ListBox x:Name="listBoxCars" Width="180" Margin="5,0" ItemTemplate="{StaticResource carListItemViewTemplate}"></ListBox>
    </StackPanel>
</Window>

3.指定数据源

View Code 

private void InitialCarList()
        {
            List<Car> cars = new List<Car>()
            {
                new Car(){ Automaker="CADILLAC",Name="CADILLAC1",Year="1990",TopSpeed="340"},
                new Car(){ Automaker="Ferrari",Name="Ferrari1",Year="1991",TopSpeed="350"},
                new Car(){ Automaker="LAMBORGHINI",Name="LAMBORGHINI1",Year="1992",TopSpeed="360"},
                new Car(){ Automaker="PORSCHE",Name="PORSCHE1",Year="1993",TopSpeed="370"}
            };

            this.listBoxCars.ItemsSource = cars;
        }

三、实例下载

https://download.csdn.net/download/qq_30725967/86507116

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WPF ListBox 绑定模板指的是在 ListBox 中使用数据绑定来绑定数据源,并使用自定义的模板来呈现数据。 以下是实现 WPF ListBox 绑定模板的步骤: 1. 创建数据源:创建一个集合类,用于存储数据源。 2. 绑定数据源:使用 ListBox 的 ItemsSource 属性将数据源绑定到 ListBox 控件上。 3. 创建数据模板:通过创建一个 DataTemplate 对象来定义自定义模板。 4. 应用模板:使用 ListBox 的 ItemTemplate 属性将模板应用到 ListBox 控件上。 下面是一个示例代码,演示如何实现 WPF ListBox 绑定模板: ``` <Window x:Class="WpfApp1.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"> <Grid> <ListBox ItemsSource="{Binding Students}"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel> <TextBlock Text="{Binding Name}" /> <TextBlock Text="{Binding Age}" /> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </Grid> </Window> ``` 在上述代码中,我们创建了一个 ListBox 控件,并将它的 ItemsSource 属性绑定到一个名为 Students 的集合类上。然后,我们创建了一个 DataTemplate 对象,并在其中定义了一个 StackPanel 和两个 TextBlock 控件,用于显示每个学生的姓名和年龄。最后,我们将模板应用到 ListBox 中,以便呈现数据。 注意:在代码中,我们使用了数据绑定和 MVVM 模式,这里不再赘述。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值