德化县住房和城乡建设局网站,东道设计地址,7有免费建网站,温州哪里有做网站的《深入浅出WPF》读书笔记.11Template机制(上)
背景
模板机制用于实现控件数据算法的内容与外观的解耦。
《深入浅出WPF》读书笔记.11Template机制(上)
模板机制
模板分类 数据外衣DataTemplate
常用场景 事件驱动和数据驱动的区别 示例代码
使用DataTemplate实现数据样式…《深入浅出WPF》读书笔记.11Template机制(上)
背景
模板机制用于实现控件数据算法的内容与外观的解耦。
《深入浅出WPF》读书笔记.11Template机制(上)
模板机制
模板分类 数据外衣DataTemplate
常用场景 事件驱动和数据驱动的区别 示例代码
使用DataTemplate实现数据样式
Window x:ClassTemplateDemo.DataTemplateViewxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:dhttp://schemas.microsoft.com/expression/blend/2008xmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006xmlns:localclr-namespace:TemplateDemomc:IgnorabledTitleDataTemplateView Height450 Width800Window.Resourceslocal:AutoMaker2PhotoPathConverter x:Keya2p/local:AutoMaker2PhotoPathConverterlocal:Name2PhotoPathConverter x:Keyn2p/local:Name2PhotoPathConverterDataTemplate x:KeydetailTemplateBorder BorderBrushBlack BorderThickness1 CornerRadius6StackPanel Margin5Image x:Nameimg1 Height250 Width400 Source{Binding Name,Converter{StaticResource n2p}}/ImageStackPanel OrientationHorizontalTextBlock TextName: FontSize25 FontWeightBold/TextBlockTextBlock x:NametblName FontSize25 FontWeightBold Text{Binding Name}/TextBlock/StackPanelStackPanel OrientationHorizontalTextBlock TextAutoMaker: Margin5/TextBlockTextBlock x:NametblAutoMaker Margin5 Text{Binding AutoMaker}/TextBlockTextBlock TextYear: Margin5/TextBlockTextBlock x:NametblYear Margin5 Text{Binding Year}/TextBlockTextBlock TextTopSpeed:: Margin5/TextBlockTextBlock x:NametblTopSpeed Margin5 Text{Binding TopSpeed}/TextBlock/StackPanel/StackPanel/Border/DataTemplateDataTemplate x:KeycarListItemBorder BorderBrushBlack CornerRadius6 BorderThickness1StackPanel Margin5 OrientationHorizontalImage Source{Binding Name,Converter{StaticResource n2p}} Width64 Height64/ImageStackPanelTextBlock Text{Binding Name}/TextBlockTextBlock Text{Binding Year}/TextBlock/StackPanel/StackPanel/Border/DataTemplate/Window.ResourcesGridGrid.ColumnDefinitionsColumnDefinition Width200*/ColumnDefinitionColumnDefinition Width100*/ColumnDefinition/Grid.ColumnDefinitionsUserControl x:NameucCarDetail Grid.Column0 ContentTemplate{StaticResource detailTemplate} Content{Binding ElementNamelistBoxCar,PathSelectedItem}/UserControlListBox x:NamelistBoxCar Grid.Column1 ItemTemplate{StaticResource carListItem}/ListBox/Grid
/Windowusing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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.Shapes;namespace TemplateDemo
{/// summary/// DataTemplateView.xaml 的交互逻辑/// /summarypublic partial class DataTemplateView : Window{public DataTemplateView(){InitializeComponent();GetCarData();}public void GetCarData(){ListCar cars new ListCar(){new Car{Nameavatar1,Year1998,AutomakerCN,TopSpeed300},new Car{Nameavatar2,Year1999,AutomakerCN,TopSpeed350},new Car{Nameavatar3,Year2000,AutomakerCN,TopSpeed400}};this.listBoxCar.ItemsSource cars;}}
}using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;namespace TemplateDemo
{public class L2BConver : IValueConverter{public object Convert(object value, Type targetType, object parameter, CultureInfo culture){return (int)value 6 ? true : false;}public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture){throw new NotImplementedException();}}
}代码说明 控件外衣ContentTemplate
用途 使用blend观看控件内部
Window x:ClassTemplateDemo.ControlTemplatexmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:dhttp://schemas.microsoft.com/expression/blend/2008xmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006xmlns:localclr-namespace:TemplateDemomc:IgnorabledTitleControlTemplate Height300 Width400Window.ResourcesStyle x:KeyRoundCornerTextBox BasedOn{x:Null} TargetTypeTextBoxSetter PropertyTemplateSetter.ValueControlTemplate TargetTypeTextBoxBorder CornerRadius5 x:Namebd SnapsToDevicePixelsTrueBorderBrush{TemplateBinding BorderBrush} BorderThickness{TemplateBinding BorderThickness} Background{TemplateBinding Background}ScrollViewer x:NamePART_ContentHost SnapsToDevicePixels{TemplateBinding SnapsToDevicePixels}/ScrollViewer/Border/ControlTemplate/Setter.Value/Setter/Style/Window.ResourcesWindow.BackgroundLinearGradientBrush StartPoint0.5,0 EndPoint0.5,1GradientStop ColorOrange Offset0/GradientStopGradientStop ColorWhite Offset1/GradientStop/LinearGradientBrush/Window.BackgroundStackPanelTextBox Width120 Height40 Style{StaticResource RoundCornerTextBox} BorderBrushBlack Margin5/TextBoxTextBox Width120 Height40 Style{StaticResource RoundCornerTextBox} BorderBrushBlack Margin5/TextBoxButton Width120 Height40 Content点击一下 Margin5/Button/StackPanel
/WindowWindow x:ClassTemplateDemo.PanelTemplatexmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:dhttp://schemas.microsoft.com/expression/blend/2008xmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006xmlns:localclr-namespace:TemplateDemomc:IgnorabledTitlePanelTemplate Height450 Width800GridListBoxListBox.ItemsPanelItemsPanelTemplateStackPanel OrientationVertical/StackPanel /ItemsPanelTemplate/ListBox.ItemsPanelTextBlock Text郭靖/TextBlockTextBlock Text黄蓉/TextBlockTextBlock Text杨康/TextBlockTextBlock Text穆念慈/TextBlock/ListBox/Grid
/WindowGit地址
GitHub - wanghuayu-hub2021/WpfBookDemo: 深入浅出WPF的demo