其他分享
首页 > 其他分享> > DataTemplate 和 ControlTemplate 详细应用

DataTemplate 和 ControlTemplate 详细应用

作者:互联网

将 Template 应用给应用目标有两种方法,一种是给每个控件设定Template/ContentTemplate/ItemsTemplate/CellTemplate,不想设定的就不设置。另外一种就是 整体应用,把Template设置到 某种指定的控件或者数据上

把ControlTemplate应用到所有目标上,需要借助 Style,但是不能设定 x:key

 

 

<Window.Resources>
        <Style TargetType="{x:Type TextBox}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate>
                        <Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" 
    						Background="{TemplateBinding Background}" 
    						SnapsToDevicePixels="True" CornerRadius="8">
                            <ScrollViewer x:Name="PART_ContentHost" Focusable="false" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"/>
                            </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Setter Property="Margin" Value="5"></Setter>
            <Setter Property="BorderBrush" Value="Red"></Setter>
            <Setter Property="Height" Value="30"></Setter>
        </Style>
    </Window.Resources>
    <StackPanel>
        <TextBox></TextBox>
        <TextBox></TextBox>
        <TextBox Style="{x:Null}"></TextBox>
    </StackPanel>

  

把DataTemplate 应用到 某个数据类型上 ,通过设定 DataType来实现。同样,也不能设定 x:key

<Window x:Class="WpfDemo.StyleDemo1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfDemo"
        xmlns:sys="clr-namespace:System.Collections;assembly=mscorlib"
        mc:Ignorable="d"
        Title="StyleDemo1" Height="300" Width="300">
    <Window.Resources>
        <DataTemplate DataType="{x:Type local:Employee}">
            <Grid>
                <StackPanel Orientation="Horizontal">
                    <Grid>
                        <Rectangle Stroke="Yellow" Fill="Orange" Width="{Binding age}"></Rectangle>
                        <TextBlock Text="{Binding name}"></TextBlock>
                    </Grid>
                    <TextBlock Text="{Binding age}"></TextBlock>
                </StackPanel>
            </Grid>
        </DataTemplate>
        <sys:ArrayList x:Key="ds">
            <local:Employee name="張三" age="20"></local:Employee>
            <local:Employee name="李四" age="30"></local:Employee>
            <local:Employee name="王五" age="40"></local:Employee>
        </sys:ArrayList>
    </Window.Resources>
    <StackPanel>
        <ListBox ItemsSource="{StaticResource ds}" Height="73.662"></ListBox>
        <ComboBox ItemsSource="{StaticResource ds}" Height="18.903"></ComboBox>
    </StackPanel>
</Window>

  

 

标签:key,详细,设定,Template,控件,应用,ControlTemplate,DataTemplate
来源: https://www.cnblogs.com/JerryZhang320/p/15195520.html