自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

教你如何理解WPF中的Template類

開發(fā) 后端
本文將你教你如何理解WPF中的Template類。文章由以下部分組成:解釋Template定義、舉Template結(jié)構(gòu)、舉例各種Template、我眼中的最佳實(shí)踐。
解釋Template定義

Template用于定義控件結(jié)構(gòu)(Visual Tree),和Style有點(diǎn)容易混淆,每個(gè)控件初始沒有Style屬性,而在WPF中所有的控件都有默認(rèn)的Template。

列舉Template結(jié)構(gòu)

image

FrameworkTemplate & ControlTemplate

ControlTemplate 添加了兩個(gè)屬性: TargetType,Triggers

 

舉例各種Template 1. ControlTempalte - 自定義BUTTON

"WpfApplication1.Window2"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window2" Height="300" Width="300">
    
        "ItemsPanelTemplate1">
            "True" Orientation="Horizontal"/>
        
    
    
        "10,10,22,73" Name="listBox1" ItemsPanel="{DynamicResource ItemsPanelTemplate1}" />
    

效果:

image

2. ItemsTemplate - 自定義ListBox對齊方式 

"WpfApplication1.Window2"    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window2" Height="300" Width="300">
    
        "ItemsPanelTemplate1">
            "True" Orientation="Horizontal"/>
        
    
    
        "10,10,22,73" Name="listBox1" ItemsPanel="{DynamicResource ItemsPanelTemplate1}" />
    

TIP:     VirtualizingStackPanel 可以替換為其他ItemsPanel,如WrapPanel,StackPanel,UnifromGrid等。

3. DataTemplate - 自定義ListBox ItemTemplate

頁面代碼

"WpfApplication1.Window2"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window2" Height="351" Width="473" xmlns:WpfApplication1="clr-namespace:WpfApplication1" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d">
    
        "ItemsPanelTemplate1">
            "Auto" Height="Auto" Columns="5" Rows="3"/>
        
        "DataTemplate1">
            "111" Width="119" x:Name="grid" Background="#33ECF678">
                "Left" Margin="8,8,0,0" VerticalAlignment="Top" Width="103" Height="96" OpacityMask="#FFFFFFFF" Source="{Binding Path=ImageSource, Mode=Default}"/>
            
        
        "MyDataDS" ObjectType="{x:Type WpfApplication1:MyData}" d:IsDataSource="True"/>
    
    
        "listbox1" Margin="10,10,8,8" ItemsSource="{Binding}"  ItemsPanel="{DynamicResource ItemsPanelTemplate1}" ItemTemplate="{DynamicResource DataTemplate1}" Background="#3FDEF5E3" />
    

邏輯代碼

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.Shapes;
using System.IO;

namespace WpfApplication1
{
    /// 
    /// Interaction logic for Window2.xaml
    /// 
    public partial class Window2 : Window
    {
        public Window2()
        {

            InitializeComponent();


            listbox1.DataContext = GetPictures();
            
            
        }

        private IEnumerable GetPictures()
        {
            foreach(string str in Directory.GetFiles(@"C:\Users\Public\Pictures\Sample Pictures"))
                yield return new MyData() {ImageSource = str};
        }


    }

    public class MyData
    {
        public string ImageSource { get; set; }
    }
}

效果

image

我眼中的最佳實(shí)踐

1. Please USE Blend to Customize Templates

請使用Blend來自定義模板

2. USE  BasedOn Property Of Style

使用Style上的BasedOn屬性

3. 使用共享資源,達(dá)到樣式&Template重用

HOW: 在App.xaml中設(shè)置獨(dú)立資源,或者使用外部資源

【編輯推薦】

  1. 詳解Silverlight和WPF互相擴(kuò)展
  2. Java代碼的靜態(tài)編譯和動(dòng)態(tài)編譯中的問題比較
  3. 為WPF項(xiàng)目創(chuàng)建單元測試
責(zé)任編輯:彭凡 來源: cnblogs
相關(guān)推薦

2009-09-23 10:14:22

Hibernate

2024-03-15 09:44:17

WPFDispatcherUI線程

2020-08-23 11:32:21

JavaScript開發(fā)技術(shù)

2009-11-24 09:23:14

生成PHP類文件

2021-11-26 00:05:56

RabbitMQVirtualHostWeb

2021-05-06 09:18:18

SQL自連接數(shù)據(jù)

2022-08-31 12:57:58

PythonTemplate文件報(bào)告

2009-12-24 16:57:53

WPF密碼

2024-04-17 09:27:22

WPF工具Template

2010-10-29 11:05:44

職場

2021-02-25 10:20:26

Java接口代碼

2009-12-23 10:20:27

WPF類層次

2015-03-23 09:33:43

Java抽象類Java接口Java

2014-03-12 10:19:54

iOS對象

2016-10-25 14:27:32

metaclasspython

2011-04-14 15:55:35

WPF.NET

2011-04-28 09:23:36

REST

2020-10-21 08:05:45

Scrapy

2022-04-27 08:55:01

Spring外部化配置

2024-09-05 17:45:33

Vue函數(shù)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號