WPF中关于DataTemplate的示例(深入浅出WPF)

34 篇文章 9 订阅

第一上,UI代码

<Window x:Class="WpfApp1.ImageWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="ImageWindow" Height="450" Width="800">

    <Window.Resources>
        <local:PathToImage x:Key="pathToImage"/>
        
        <DataTemplate x:Key="_carDetailViewTemplate">
            <Border BorderBrush="Black" BorderThickness="1" CornerRadius="6">
                <StackPanel Margin="5">
                    <Image Width="400" Height="250" Source="{Binding Path=ImagePath,Converter={StaticResource pathToImage}}"/>
                    <StackPanel Orientation="Horizontal" Margin="5,0">
                        <TextBlock Text="Name:" FontWeight="Bold" FontSize="20"/>
                        <TextBlock Text="{Binding Path=Name}" FontSize="20" Margin="5,0"/>
                    </StackPanel>
                </StackPanel>
            </Border>
        </DataTemplate>

        <DataTemplate x:Key="_carListItemViewTemplate">
            <Grid Margin="2">
                <StackPanel Orientation="Horizontal">
                    <Image Source="{Binding Path=Automarker,Converter={StaticResource pathToImage}}" Width="64" Height="64"/>
                    <StackPanel Margin="5,10">
                        <TextBlock Text="{Binding Name}" FontSize="16" FontWeight="Bold"/>
                        <TextBlock Text="{Binding Year}" FontSize="14"/>
                    </StackPanel>
                </StackPanel>
            </Grid>
        </DataTemplate>
    </Window.Resources>
    <StackPanel Orientation="Horizontal" Margin="5">
        <UserControl ContentTemplate="{StaticResource _carDetailViewTemplate}" Content="{Binding Path=SelectedItem,ElementName=_listBoxCars}"/>
        <ListBox x:Name="_listBoxCars" Width="180" Margin="5,0" ItemTemplate="{StaticResource _carListItemViewTemplate}"/>
    </StackPanel>
</Window>

第二步,C#代码的实现

 public class Car
    {
        public string Name { get; set; }
        public string ImagePath { get; set; }
        public string Automarker { get; set; }
        public string Year { get; set; }
    }
    public class PathToImage : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string url = value as string;
            return (new BitmapImage(new Uri(url, UriKind.RelativeOrAbsolute)));
        }

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

    /// <summary>
    /// ImageWindow.xaml 的交互逻辑
    /// </summary>
    public partial class ImageWindow : Window
    {
        List<Car> cars = new List<Car>();
        public ImageWindow()
        {
            InitializeComponent();
            cars.Add(new Car { Name = "X-3SD", ImagePath = @"/Images/1.png", Automarker = @"/Images/1.png", Year = "2019" });
            cars.Add(new Car { Name = "S-010", ImagePath = @"/Images/2.png", Automarker = @"/Images/2.png", Year = "2014" });
            cars.Add(new Car { Name = "B-SS4", ImagePath = @"/Images/3.png", Automarker = @"/Images/3.png", Year = "2021" });
            cars.Add(new Car { Name = "X-888", ImagePath = @"/Images/4.png", Automarker = @"/Images/4.png", Year = "2008" });
            _listBoxCars.ItemsSource = cars;

        }
    }

点小图出大图的效果

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值