深入浅出WPF-笔记(2015.04.04)

     Style是一组<Setter>,也就是一组属性设置器。

    ・使用Style时,如果Value的值比较简单,那就直接用Attribute值来表示,如果Value值不能用一个简单地字符串描述就需要使用XAML的属性对象语法。

    ControlTemplate最终将被应用到一个控件上,称这个控件为模板目标控件或模板化控件(Templated Control),ControlTemplate里的控件可以使用TemplateBinding将自己的属性值关联在目标控件的某个属性上,必要的时候还可以田间Converter.

      WPF的UI元素可以看作两棵树--LogicalTree和VisualTree,这两棵树的交点就是ControlTempalte。如果把界面上的控件元素看作是一个结点,那元素们构成的就是LogicalTree,如果把控件内部由ControlTemplate生成的控件也算上,那构成的就是VisualTree。换句话说,在LogicalTree上导航不会进入空间内部,而在VisualTree上导航则可检索到控件内部由ControlTemplate生成的子级控件。

    ItemsControl具有一个名为ItemsPanel的属性,其数据类型为ItemsPanelTempalte。ListBox的内容横向显示示例如下:

<Window x:Class="MyTestWpfApplication.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="RuntimePopup" Height="300" Width="300">
    <Grid Margin="6">
        <ListBox>
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal"/>
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
            <TextBlock Text="Allan"/>
            <TextBlock Text="Kevin"/>
            <TextBlock Text="Drew"/>
            <TextBlock Text="Timothy"/>
        </ListBox>
    </Grid>
</Window>

    凡是Template,最终都是要作用在控件上的,这个控件就是Tempalte的目标控件,也叫模板化控件(Templated Control)。

    决定控件外观的是ControlTempalte,决定数据外观的是DataTemplate,它们正是Control类的Template和ContentTemplate两个属性的值。

    由ControlTemplate生成的控件树其树根就是ControlTemplate的目标控件,此模板化控件的Template属性值就是这个ControlTemplate控件实例;由DataTemplate生成的控件树其树根是一个ContentPresenter控件,此模板化控件的ControlTemplate属性值就是这个DataTemplate实例。示例:

XAML代码:

<Window x:Class="MyTestWpfApplication.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="RuntimePopup" Height="300" Width="300">
    <Window.Resources>
        <Style TargetType="{x:Type TextBox}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TextBox}">
                        <!--Code-->
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Setter Property="Margin" Value="5"/>
            <Setter Property="BorderBrush" Value="Black"/>
            <Setter Property="Height" Value="25"/>
        </Style>
    </Window.Resources>
    <StackPanel>
        <TextBox/>
        <TextBox/>
        <TextBox Style="{x:Null}"/>
    </StackPanel>
</Window>

==============================================================================

    把ControlTemplate应用在所有目标上需要借助Style来实现,但Style不能标记x:key;把DataTemplate应用在某个数据类型上的方法是设置DataTemplate的DataType属性,并且DataTemplate作为资源时也不能带有x:Key标记。示例:

XAML代码:

<Window x:Class="MyTestWpfApplication.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:MyTestWpfApplication"
        xmlns:c="clr-namespace:System.Collections;assembly=mscorlib"
        Title="RuntimePopup" Height="300" Width="300">
    <Window.Resources>
    <!--DataTemplate-->
        <DataTemplate DataType="{x:Type local:Unit}">
            <Grid>
                <StackPanel Orientation="Horizontal">
                    <Grid>
                        <Rectangle Stroke="Yellow" Fill="Orange" Width="{Binding Price}"/>
                        <TextBox Text="{Binding Year}"/>
                    </Grid>
                    <TextBlock Text="{Binding Price}" Margin="5, 0"/>
                </StackPanel>
            </Grid>
        </DataTemplate>
       
        <!--数据源-->
        <c:ArrayList x:Key="ds">
            <local:Unit Year="2001" Price="100"/>
            <local:Unit Year="2002" Price="120"/>
            <local:Unit Year="2003" Price="140"/>
            <local:Unit Year="2004" Price="160"/>
            <local:Unit Year="2005" Price="180"/>
            <local:Unit Year="2006" Price="200"/>
        </c:ArrayList>
    </Window.Resources>
    <StackPanel>
        <ListBox ItemsSource="{StaticResource ResourceKey=ds}"/>
        <ComboBox ItemsSource="{StaticResource ResourceKey=ds}" Margin="5"/>
    </StackPanel>
</Window>

C#代码:

    public class Unit
    {
        public int Price { get; set; }
        public string Year { get; set; }
    }

    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

    }

 

你可能感兴趣的:(template,WPF)