WPF的模版
2024-08-25 10:36:05
此例子来自《深入浅出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文件
主窗体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="",TopSpeed=""},
new Car(){Automaker="VolksWagen",Name="Golf GTI",Year="",TopSpeed=""},
new Car(){Automaker="VolksWagen",Name="Jetta",Year="",TopSpeed=""},
new Car(){Automaker="VolksWagen",Name="Pheaton",Year="",TopSpeed=""}
};
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="",TopSpeed=""},
new Car(){Automaker="VolksWagen",Name="Golf GTI",Year="",TopSpeed=""},
new Car(){Automaker="VolksWagen",Name="Jetta",Year="",TopSpeed=""},
new Car(){Automaker="VolksWagen",Name="Pheaton",Year="",TopSpeed=""}
};
listBoxCars.ItemsSource = carList;
}
}
}
Car类
下面是两个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
}
}
AutomakerToLogoPathConverter
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
}
}
NameToPhotoPathConverter
下面是文档结构:
最新文章
- 【记录】vmware fusion 7 windows 10 unidentified network
- Windows10环境配置nat123端口映射访问mysql
- xmpp SASL 定义
- 科技部专家王涌天:移动AR头显将“让人类重新站起来”
- session,cookie
- Android之开启手机系统自带铃声
- 关于C和C++动态链接库的几个问题
- HDU 4651 Partition 整数划分,可重复情况
- ocp 1Z0-051 141-175题解析
- 在bootloader及IAP中使用zlib解压缩
- 自定义Excel导出简易组件
- NET Core控制反转(IoC)
- js根据毫米/厘米算像素px
- css点滴1—八种方式实现元素垂直居中
- sql server和oracle数据库
- JSF web.xml的各类参数属性配置
- SSH无法连上CentOS7的问题
- 2018.09.28 hdu5435A serious math problem(数位dp)
- java判断字符串编码
- Jmeter基础元件
热门文章
- c# 请问如何将四个RadioButton分成两组?
- PAT---1050. String Subtraction (20)
- 像C++一样写JavaScript
- [Redux] Filtering Redux State with React Router Params
- Block使用变量,让你的程序看起来清晰!
- 2440test 裸机測试 调试不进main 设置改动方法
- 关于css的兼容
- Bootstrap学习——起步
- WisDom.Net 框架设计(五) 权限设计
- 在VS中关于MySQL的相关问题