首页 > 代码库 > WPF的模版
WPF的模版
此例子来自《深入浅出WPF》,刘铁猛。
VS2010
源码1:不使用Bingding
源码2:使用Binding
下面展示一些代码:
主窗体XAML代码:
<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfApplication1" Title="MainWindow" Height="350" Width="623" ResizeMode="NoResize"> <Window.Resources> <local:AutomakerToLogoPathConverter x:Key="a2l"></local:AutomakerToLogoPathConverter> <local:NameToPhotoPathConverter x:Key="n2p"></local:NameToPhotoPathConverter> <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 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,10"> <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>
主窗体XAML文件的后台:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Navigation;using System.Windows.Shapes;namespace WpfApplication1{ /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); InitialCarList(); } private void InitialCarList() { List<Car> carList = new List<Car>() { new Car(){Automaker="VolksWagen",Name="Beetle",Year="1990",TopSpeed="320"}, new Car(){Automaker="VolksWagen",Name="Golf GTI",Year="1988",TopSpeed="220"}, new Car(){Automaker="VolksWagen",Name="Jetta",Year="2002",TopSpeed="220"}, new Car(){Automaker="VolksWagen",Name="Pheaton",Year="2005",TopSpeed="180"} }; listBoxCars.ItemsSource = carList; } }}
实体Car类:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Navigation;using System.Windows.Shapes;namespace WpfApplication1{ /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); InitialCarList(); } private void InitialCarList() { List<Car> carList = new List<Car>() { new Car(){Automaker="VolksWagen",Name="Beetle",Year="1990",TopSpeed="320"}, new Car(){Automaker="VolksWagen",Name="Golf GTI",Year="1988",TopSpeed="220"}, new Car(){Automaker="VolksWagen",Name="Jetta",Year="2002",TopSpeed="220"}, new Car(){Automaker="VolksWagen",Name="Pheaton",Year="2005",TopSpeed="180"} }; listBoxCars.ItemsSource = carList; } }}
下面是两个Converter:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Windows.Data;using System.Windows.Media.Imaging;namespace WpfApplication1{ class AutomakerToLogoPathConverter:IValueConverter { #region IValueConverter 成员 public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { string uriStr = string.Format(@"/Resources/Logos/{0}.jpg",(string)value); return new BitmapImage(new Uri(uriStr, UriKind.Relative)); } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } #endregion }}
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Windows.Data;using System.Windows.Media.Imaging;namespace WpfApplication1{ class NameToPhotoPathConverter:IValueConverter { #region IValueConverter 成员 public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { string uriStr = string.Format(@"/Resources/Images/{0}.jpg",(string)value); return new BitmapImage(new Uri(uriStr, UriKind.Relative)); } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } #endregion }}
下面是文档结构:
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。