您的位置:首页 > 其它

[Silverlight学习笔记]关于Silverlight的Template(待续)

2011-03-14 13:54 591 查看
总结一下N种Template:

1、ControlTemplate:ControlTemplate是控件模板,其定义如下:

/**
System.Windows.Controls 的成员
摘要:
定义作为控件模板应用的元素树。
**/
public sealed class ControlTemplate : System.Windows.FrameworkTemplate
{...}


MSDN官方描述如下:

/**
ControlTemplate 允许您指定控件的可视结构。 控件作者可以定义默认的 ControlTemplate,而应用程序作者可以重写 ControlTemplate 以重新构建控件的可视结构。
控件模板化是 WPF 样式和模板化模型提供的众多功能中的一种。 样式和模板化模型为您提供了很大的灵活性,在许多情况下,您都不需要编写自己的控件。 如果您是希望更改控件的可视化或替换现有控件的 ControlTemplate 的应用程序作者,请参见 样式设置和模板化 主题来获取示例和深入讨论。
如果您要编写自己的控件,请参见控件创作概述中的“创建自定义控件”。
ControlTemplate 旨在用作实现详细信息的独立单元,对于外部用户和对象(包括样式)不可见。 仅可以从同一控件模板的内部操作此控件模板的内容。
**/


其用于定义所有继承自Control控件的模板,作用于Control类的Template属性。下面显示了一个ButtonStyle,它用于设置Button的 ControlTemplate:

<Style TargetType="Button">
<!--Set to true to not get any properties from the themes.-->
<Setter Property="OverridesDefaultStyle" Value="True"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid>
<Ellipse Fill="{TemplateBinding Background}"/>
<ContentPresenter HorizontalAlignment="Center"
VerticalAlignment="Center"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>


2、DataTemplate:数据模板,描述数据对象的可视结构。其定义如下:

/**
System.Windows 的成员
摘要:
描述数据对象的可视结构。
**/
public class DataTemplate : System.Windows.FrameworkTemplate
{...}


作用于所有继承自ContentControl的内容控件的ContentTemplate属性和所有继承自ItemsControl的列表控件的ItemTemplate属性,即用于设置控件的数据内容。官方MSDN描述如下:

/**
通常使用 DataTemplate 指定数据的直观表示。当您将 ItemsControl(如 ListBox)绑定到整个集合时,DataTemplate 对象尤其有用。如果没有特殊说明,ListBox 将在集合中显示对象的字符串表示形式。在此情况下,可以使用 DataTemplate 定义数据对象的外观。DataTemplate 的内容变成数据对象的可视结构。
可以在 DataTemplate 中使用数据绑定。例如,假定 ListBox 绑定到 Customer 对象的集合,并且将 ItemTemplate 属性设置为 DataTemplate。创建 ListBox 时,将为集合中的每个 Customer 创建一个 ListBoxItem,并将 ListBoxItem 的 DataContext 设置为相应的客户。也就是说,第一个 ListBoxItem 的 DataContext 设置为第一个客户,第二个 ListBoxItem 的 DataContext 设置为第二个客户,依此类推。可以将 DataTemplate 中的元素绑定到 Customer 对象的属性。
还可以使用 DataTemplate 在多个 ContentControl 对象之间共享 UIElement 对象。例如,假设需要应用程序上的多个按钮具有相同的图形。可以创建一个包含此图形的 DataTemplate,并将它用作这些按钮的 ContentTemplate。有关更多信息,请参见 ContentControl.ContentTemplate。
可以将 DataTemplate 作为 object.ItemTemplate 属性元素的直接子级。还可以定义一个 DataTemplate 作为资源,然后将该资源作为 ItemTemplate 属性的值引用。
定义用于创建数据模板的内容的 XAML 用法不作为可设置的属性公开。这是内置于 DataTemplate 对象元素的 XAML 处理的特殊行为。
**/


其相关示例如下:

<Button  Height="23" Name="btnRefresh" Width="75" Click="btnRefresh_Click" Canvas.Left="208" Canvas.Top="98" >
<Button.Resources>
<DataTemplate x:Key="DataTemplate1">
<TextBlock Text="刷新"/>
</DataTemplate>
</Button.Resources>
<Button.ContentTemplate>
<StaticResource ResourceKey="DataTemplate1"/>
</Button.ContentTemplate>
</Button>


或者上面代码也可以这样写:

<Button  Height="23" Name="btnRefresh" Width="75" Click="btnRefresh_Click" Canvas.Left="208" Canvas.Top="98" >
<Button.ContentTemplate>
<DataTemplate>
<TextBlock Text="刷新"/>
</DataTemplate>
</Button.ContentTemplate>
</Button>


3、ContentTemplate:ContentTemplate内容模板,该属性返回的是一个DataTemplate类型。其定义如下:

/*System.Windows.Controls.ContentControl 的成员
摘要:
获取或设置用于显示 System.Windows.Controls.ContentControl 内容的数据模板。
返回值:
用于显示 System.Windows.Controls.ContentControl 内容的数据模板。
*/
public System.Windows.DataTemplate ContentTemplate { set; get; }


所有继承自ContentControl的内容控件都有ContentTemplate属性,用于设置内容控件的显示内容,比内容控件的Content属性更加灵活,具有CSS样式的特征,即一个模板可以作用于不同的内容控件。如:

<Button  Height="23" Name="btnRefresh" Width="75" Click="btnRefresh_Click" Canvas.Left="208" Canvas.Top="98" >
<Button.ContentTemplate>
<DataTemplate>
<TextBlock Text="刷新"/>
</DataTemplate>
</Button.ContentTemplate>
</Button>


4、ItemTemplate:获取或设置用于显示每个项的 DataTemplate。其定义如下:

/**
System.Windows.Controls.ItemsControl 的成员
摘要:
获取或设置用于显示每个项的 System.Windows.DataTemplate。
返回值:
指定数据对象的可视化的模板。默认为 null。
**/
public System.Windows.DataTemplate ItemTemplate { set; get; }


ItemTemplate是所有继承自ItemsControl的列表控件都有的属性,它用于定义列表控件中的各子项的统一模板。其相关示例如下:

<ListBox Height="183" HorizontalAlignment="Left" Margin="360,12,0,0" Name="listBoxUser" VerticalAlignment="Top" Width="148" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock><Bold>用户名:</Bold><Run Text="{Binding UserName}"/></TextBlock>
<TextBlock><Bold>密码:</Bold><Run Text="{Binding Password}"/></TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>


5、ItemsPanelTemplate:指定一个面板,这个面板是ItemsPresenter为ItemsControl的项创建的布局的面板。

/**
System.Windows.Controls 的成员
摘要:
指定 System.Windows.Controls.ItemsPresenter 为 System.Windows.Controls.ItemsControl 的项的布局创建的面板。
**/
public sealed class ItemsPanelTemplate : System.Windows.FrameworkTemplate
{..}


用ItemsPanelTemplate就可以设置ItemsControl中项的排列方式,比如默认情况ListBox的项都是竖直排列的,而当将某一布局对应的ItemsPanelTemplate作用于ListBox的ItemsPanel属性时,这个默认竖直排列的布局就会被更改,可能可以水平排列,也可以像照片墙那样混乱排列。其官方描述如下:

/**
ItemsPanelTemplate 指定用于项的布局的面板。 GroupStyle 具有一个类型为 ItemsPanelTemplate 的 Panel 属性。 ItemsControl 类型具有一个类型为 ItemsPanelTemplate 的 ItemsPanel 属性。
每种 ItemsControl 类型都有默认的 ItemsPanelTemplate。 对于 ItemsControl 类,默认的 ItemsPanel 值为指定 StackPanel 的 ItemsPanelTemplate。 对于 ListBox,默认值使用 VirtualizingStackPanel。 对于 MenuItem,默认值使用 WrapPanel。 对于 StatusBar,默认值使用 DockPanel。
**/


若要创建水平 ListBox,可以创建一个指定水平 StackPanel 的模板并将其设置为 ItemsPanel 属性。 下面的示例演示创建水平 ListBox 的 ListBoxStyle。

<Style TargetType="ListBox">
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"
VerticalAlignment="Center"
HorizontalAlignment="Center"/>
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
</Style>


或者可以直接这样写:

<ListBox Height="183" HorizontalAlignment="Left" Margin="360,12,0,0" Name="listBoxUser" VerticalAlignment="Top" Width="148" >
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐