.NET: WPF Template

Data Template:

要做一个listBox,里面有车子的简单信息,点了里面的item后就会显示详细信息。

car class:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6
 7 namespace WpfApplication1
 8 {
 9     public class Car
10     {
11         public string Automaker { get; set; }
12         public string Name { get; set; }
13         public string Year { get; set; }
14         public string TopSpeed { get; set; }
15     }
16 }

carListItemView.xaml:

 1 <UserControl x:Class="WpfApplication1.CarListItemView"
 2              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 5              xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
 6              mc:Ignorable="d"
 7              d:DesignHeight="50" d:DesignWidth="300">
 8     <Grid Margin="2">
 9         <StackPanel Orientation="Horizontal">
10             <Image x:Name="imageLogo" Grid.RowSpan="3" Width="64" Height="64"/>
11             <StackPanel Margin="5, 10">
12                 <TextBlock x:Name="textBlockName" FontSize="16" FontWeight="Bold"/>
13                 <TextBlock x:Name="textBlockYear" FontSize="14"/>
14             </StackPanel>
15         </StackPanel>
16     </Grid>
17 </UserControl>

carListItemView.xaml.cs:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using System.Windows;
 7 using System.Windows.Controls;
 8 using System.Windows.Data;
 9 using System.Windows.Documents;
10 using System.Windows.Input;
11 using System.Windows.Media;
12 using System.Windows.Media.Imaging;
13 using System.Windows.Navigation;
14 using System.Windows.Shapes;
15
16 namespace WpfApplication1
17 {
18     /// <summary>
19     /// Interaction logic for CarListItemView.xaml
20     /// </summary>
21     public partial class CarListItemView : UserControl
22     {
23         private Car car;
24         public CarListItemView()
25         {
26             InitializeComponent();
27         }
28         public Car Car
29         {
30             get { return car; }
31             set
32             {
33                 car = value;
34                 this.textBlockName.Text = car.Name;
35                 this.textBlockYear.Text = car.Year;
36                 string uriStr = string.Format(@"/Resource/Logos/{0}.jpg", car.Automaker);
37                 this.imageLogo.Source = new BitmapImage(new Uri(uriStr, UriKind.Relative));
38             }
39         }
40     }
41 }

carDetailView.xaml:

 1 <UserControl x:Class="WpfApplication1.CarDetailView"
 2              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 5              xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
 6              mc:Ignorable="d"
 7              d:DesignHeight="500" d:DesignWidth="500">
 8     <Border BorderBrush="Black" BorderThickness="1" CornerRadius="6">
 9         <StackPanel Margin="5">
10             <Image x:Name="imagePhoto" Width="200" Height="250"/>
11             <StackPanel Orientation="Horizontal" Margin="5, 0">
12                 <TextBlock Text="Name:" FontWeight="Bold" FontSize="20"/>
13                 <TextBlock x:Name="textBlockName" FontSize="20" Margin="5, 0"/>
14             </StackPanel>
15             <StackPanel Orientation="Horizontal" Margin="5, 0">
16                 <TextBlock Text="Automaker:" FontWeight="Bold"/>
17                 <TextBlock x:Name="textBlockAutomaker" Margin="5, 0"/>
18                 <TextBlock Text="Year:" FontWeight="Bold"/>
19                 <TextBlock x:Name="textBlockYear" Margin="5, 0"/>
20                 <TextBlock Text="Top Speed:" FontWeight="Bold"/>
21                 <TextBlock x:Name="textBlockTopSpeed" Margin="5, 0"/>
22             </StackPanel>
23         </StackPanel>
24     </Border>
25 </UserControl>

carDetailView.xaml.cs

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using System.Windows;
 7 using System.Windows.Controls;
 8 using System.Windows.Data;
 9 using System.Windows.Documents;
10 using System.Windows.Input;
11 using System.Windows.Media;
12 using System.Windows.Media.Imaging;
13 using System.Windows.Navigation;
14 using System.Windows.Shapes;
15
16 namespace WpfApplication1
17 {
18     /// <summary>
19     /// Interaction logic for CarDetailView.xaml
20     /// </summary>
21     public partial class CarDetailView : UserControl
22     {
23         public CarDetailView()
24         {
25             InitializeComponent();
26         }
27         private Car car;
28         public Car Car
29         {
30             get { return car; }
31             set
32             {
33                 car = value;
34                 this.textBlockName.Text = car.Name;
35                 this.textBlockYear.Text = car.Year;
36                 this.textBlockTopSpeed.Text = car.TopSpeed;
37                 this.textBlockAutomaker.Text = car.Automaker;
38                 string uriStr = string.Format(@"/Resource/Logos/{0}.jpg", car.Name);
39                 this.imagePhoto.Source = new BitmapImage(new Uri(uriStr, UriKind.Relative));
40             }
41         }
42     }
43 }

MainWindow.xaml:

 1 <Window x:Class="WpfApplication1.MainWindow"
 2         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4         xmlns:local="clr-namespace:WpfApplication1"
 5         Title="MainWindow" Height="350" Width="623">
 6     <StackPanel Orientation="Horizontal" Margin="5">
 7         <local:CarDetailView x:Name="detailView"/>
 8         <ListBox x:Name="listBoxCars" Width="180" Margin="5, 0"
 9                  SelectionChanged="listBoxCars_SelectionChanged"/>
10     </StackPanel>
11 </Window>

MainWindow.xaml.cs:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using System.Windows;
 7 using System.Windows.Controls;
 8 using System.Windows.Data;
 9 using System.Windows.Documents;
10 using System.Windows.Input;
11 using System.Windows.Media;
12 using System.Windows.Media.Imaging;
13 using System.Windows.Navigation;
14 using System.Windows.Shapes;
15
16 namespace WpfApplication1
17 {
18     /// <summary>
19     /// Interaction logic for MainWindow.xaml
20     /// </summary>
21     public partial class MainWindow : Window
22     {
23         public MainWindow()
24         {
25             InitializeComponent();
26             InitialCarList();
27         }
28
29         private void InitialCarList()
30         {
31             List<Car> carList = new List<Car>()
32             {
33                 new Car(){Automaker="eclipse", Name="eclipse", Year="2010", TopSpeed="200"},
34                 new Car(){Automaker="google", Name="google", Year="2013", TopSpeed="400"},
35             };
36
37             foreach(Car car in carList)
38             {
39                 CarListItemView view = new CarListItemView();
40                 view.Car = car;
41                 this.listBoxCars.Items.Add(view);
42             }
43         }
44
45         private void listBoxCars_SelectionChanged(object sender, SelectionChangedEventArgs e)
46         {
47             CarListItemView view = e.AddedItems[0] as CarListItemView;
48             if (view != null)
49             {
50                 this.detailView.Car = view.Car;
51             }
52         }
53     }
54 }

这种方式的编程跟传统Win Form的编程方法并没有什么区别,只是语言换成了xaml,思维方式并没有改变,还是事件驱动

转换下思维,listBox里的item作为target绑定car类的source,对于detailView里也是一样。用数据绑定的方式来做,xaml需要写好data template, 而cs文件就非常简单了,因为不需要关心UI

car.cs

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using System.Globalization;
 7 using System.Windows;
 8 using System.Windows.Controls;
 9 using System.Windows.Data;
10 using System.Windows.Documents;
11 using System.Windows.Input;
12 using System.Windows.Media;
13 using System.Windows.Media.Imaging;
14 using System.Windows.Navigation;
15 using System.Windows.Shapes;
16
17 namespace WpfApplication2
18 {
19     public class Car
20     {
21         public string Automaker { get; set; }
22         public string Name { get; set; }
23         public string Year { get; set; }
24         public string TopSpeed { get; set; }
25     }
26
27     public class AutomakerToLogoPathConverter : IValueConverter
28     {
29         public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
30         {
31             string uriStr = string.Format(@"/Resource/Logos/{0}.jpg", (string)value);
32             return new BitmapImage(new Uri(uriStr, UriKind.Relative));
33         }
34         public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
35         {
36             throw new NotImplementedException();
37         }
38     }
39     public class NameToPhotoPathConverter : IValueConverter
40     {
41         public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
42         {
43             string uriStr = string.Format(@"/Resource/Logos/{0}.jpg", (string)value);
44             return new BitmapImage(new Uri(uriStr, UriKind.Relative));
45         }
46         public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
47         {
48             throw new NotImplementedException();
49         }
50     }
51 }

MainWindow.xaml:

 1 <Window x:Class="WpfApplication2.MainWindow"
 2         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4         xmlns:local="clr-namespace:WpfApplication2"
 5         Title="MainWindow" Height="350" Width="623">
 6     <Window.Resources>
 7         <local:AutomakerToLogoPathConverter x:Key="a21"/>
 8         <local:NameToPhotoPathConverter x:Key="n2p"/>
 9         <DataTemplate x:Key="carDetailViewTemplate">
10             <Border BorderBrush="Black" BorderThickness="1" CornerRadius="6">
11                 <StackPanel Margin="5">
12                     <Image Width="400" Height="250"
13                            Source="{Binding Name, Converter={StaticResource n2p}}"/>
14                     <StackPanel Orientation="Horizontal" Margin="5, 0">
15                         <TextBlock Text="Name:" FontWeight="Bold" FontSize="20"/>
16                         <TextBlock Text="{Binding Name}" FontSize="20" Margin="5, 0"/>
17                     </StackPanel>
18                     <StackPanel Orientation="Horizontal" Margin="5, 0">
19                         <TextBlock Text="Automaker:" FontWeight="Bold"/>
20                         <TextBlock Text="{Binding Automaker}" Margin="5, 0"/>
21                         <TextBlock Text="Year:" FontWeight="Bold"/>
22                         <TextBlock Text="{Binding Year}" Margin="5, 0"/>
23                         <TextBlock Text="Top Speed:" FontWeight="Bold"/>
24                         <TextBlock Text="{Binding TopSpeed}" Margin="5, 0"/>
25                     </StackPanel>
26                 </StackPanel>
27             </Border>
28         </DataTemplate>
29         <DataTemplate x:Key="carListItemViewTemplate">
30             <Grid Margin="2">
31                 <StackPanel Orientation="Horizontal">
32                     <Image Source="{Binding Automaker, Converter={StaticResource a21}}"
33                            Grid.RowSpan="3" Width="64" Height="64"/>
34                     <StackPanel Margin="5, 10">
35                         <TextBlock Text="{Binding Name}" FontSize="16" FontWeight="Bold"/>
36                         <TextBlock Text="{Binding Year}" FontSize="14"/>
37                     </StackPanel>
38                 </StackPanel>
39             </Grid>
40         </DataTemplate>
41     </Window.Resources>
42     <StackPanel Orientation="Horizontal" Margin="5">
43         <UserControl ContentTemplate="{StaticResource carDetailViewTemplate}"
44                      Content="{Binding SelectedItem, ElementName=listBoxCars}"/>
45         <ListBox x:Name="listBoxCars" Width="180" Margin="5, 0"
46                  ItemTemplate="{StaticResource carListItemViewTemplate}"/>
47     </StackPanel>
48 </Window>

MainWindow.xaml.cs:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using System.Windows;
 7 using System.Windows.Controls;
 8 using System.Windows.Data;
 9 using System.Windows.Documents;
10 using System.Windows.Input;
11 using System.Windows.Media;
12 using System.Windows.Media.Imaging;
13 using System.Windows.Navigation;
14 using System.Windows.Shapes;
15
16 namespace WpfApplication2
17 {
18     /// <summary>
19     /// Interaction logic for MainWindow.xaml
20     /// </summary>
21     public partial class MainWindow : Window
22     {
23         public MainWindow()
24         {
25             InitializeComponent();
26             InitialCarList();
27         }
28         private void InitialCarList()
29         {
30             List<Car> carList = new List<Car>()
31             {
32                 new Car(){Automaker="eclipse", Name="eclipse", Year="2010", TopSpeed="200"},
33                 new Car(){Automaker="google", Name="google", Year="2013", TopSpeed="400"},
34             };
35             this.listBoxCars.ItemsSource = carList;
36         }
37     }
38 }

时间: 2024-10-24 16:21:24

.NET: WPF Template的相关文章

WPF Template模版之DataTemplate与ControlTemplate【一】

WPF Template模版之DataTemplate与ControlTemplate[一] 标签: Wpf模版 2015-04-19 11:52 510人阅读 评论(0) 收藏 举报  分类: -------1.5 .NET(25)  版权声明:本文为博主郎涯工作室原创文章,未经博主允许不得转载. 目录(?)[+] WPF系统不但支持传统的Winfrom编程的用户界面和用户体验设计,更支持使用专门的设计工具Blend进行专业设计,同时还推出了以模板为核心的新一代设计理念. 1. 模板的内涵 作

WPF Template模版之寻找失落的控件【三】

“井水不犯河水”常用来形容两个组织之间界限分明.互不相干,LogicTree与控件内部这颗小树之间就保持着这种关系.换句话说,如果UI元素树上有个X:Name=“TextBox1”的控件,某个控件内部也是由Template生成的x:Name="TextBox1"的控件,它们并不冲突,LogicTree不会看到控件内部的细节,控件内部元素也不会去理会控件外面是什么值.你可能会想:“这样一来,万一我想从控件外部访问内部的控件,获取它的属性值,岂不是做不到了.”放心,WPF为我们准备了访问控

WPF Template模版之DataTemplate与ControlTemplate的关系和应用【二】

1. DataTemplate和ControlTemplate的关系 学习过DataTemplate和ControlTemplate,你应该已经体会到,控件只是数据的行为和载体,是个抽象的概念,至于它本身长成什么样子(控件内部结构),它的数据会长成什么样子(数据显示结构)都是靠Template生成的.决定控件外观的是ControlTemplate,决定数据外观的是DataTemplate,它们正是Control类的Template和ContentTemplate两个属性值 凡是Template,

WPF Template模版之Style【四】

Style直译过来就是"风格","样式".拿人来举例,人的风格指静态的外观和行为举止.同样一个人,如果留平头.穿上足球队的队服.脚蹬战靴,看上去就是一名叱咤球场的运动员:如果让他换一身笔挺的西装.穿上皮靴.再拎上一个公文包,看上去就是一个商务人士.除了从静态外观来判断一个人的风格,我们还会观察他的行为特点.比如遇到困难时,有些人很乐观,照样谈笑风生.有些人很谨慎.仔细分析问题:有些人很悲观.成天哀声叹气,这就是行为风格,行为风格是由外界刺激的反应体现出来的.说到这儿

WPF Template

ControlTemplate ControlTemplate:用于定义控件的结构和外观,这样可以将控件外观与控件功能分离开. 在xaml中ControlTemplate通常配置到Style中,通过Style控制控件的结构和外观 如果控件继承自ContentControl类,其模板将包含一个ContentPresenter类成员,ContentPresenter用于指定添加内容的位置,下面是一个Button Style,ContentPresenter的标记表明Button的Content应在G

深入浅出WPF笔记

数据层(Database,Oracle等) 业务逻辑层(Service,Data Access Layer,WCF) 表示层(WPF,Win Form,ASP.net,Silverlight) [WPF开发方法论] AS-IS:UI事件驱动程序运行(Win Form)--TO-BE:数据驱动程序运行并显示在UI上(WPF) XAML:WPF技术中专门用于设计UI的语言. 逻辑树(logical tree):不考虑控件内部的组成结构,只观察由控件组成的"树". 可视元素树(visual

WPF:在ControlTemplate中使用TemplateBinding

A bit on TemplateBinding and how to use it inside a ControlTemplate. Introductio Today I'll try to write a bit on TemplateBinding and how to use it inside a ControlTemplate.TemplateBinding is a type of binding used mainly for template scenarios. Here

WPF之Template

最近在做WPF的项目,也是刚刚接触WPF不是特别精通,关于模板的使用做了一些总结(参考<深入浅出WPF>): 模板分为两大类:ControlTemplate(决定控件长什么样子): DateTemplate(是数据内容的表现形式,一条数据显示成什么样式): 总之:Template(模板)就是“外衣”,ControlTemplate是控件的外衣,DateTemplate是内容的外衣: 一. DateTemplate常用在以下3中地方: (1)ContentTemplate的ContentTemp

WPF QuickStart系列之样式和模板(Style and Template)

在WPF桌面程序中,当我们想构建一个统一的UI表现时(在不同操作系统下,显示效果一致),此时我们就需要使用到WPF中的样式和模板技术.简单来说,如果我们需要简单的给一个Button设置宽,高,Margin等,可以使用Style来指定这一系列的属性.可以把Style理解为一个属性的集合.如果需要完全改变控件的样子,就需要使用到Template技术,相当于给控件换一层皮,不过Button还是Button,它原有的行为(Click事件)还存在.而且我们仅需要在XAML中遍可以完成对样式和模板的定义和重