use*_*603 5 c# wpf datatemplate itemtemplate controltemplate
有什么区别
ControlTemplate指定控件的视觉结构和视觉行为。您可以通过给控件一个新的ControlTemplate来自定义控件的外观。创建ControlTemplate时,可以替换现有控件的外观而不更改其功能。例如,您可以使应用程序中的按钮变为圆形而不是默认的正方形,但是该按钮仍会引发Click事件。
ControlTemplate的示例为
创建一个按钮
<Button Style="{StaticResource newTemplate}"
Background="Navy"
Foreground="White"
FontSize="14"
Content="Button1"/>
Run Code Online (Sandbox Code Playgroud)
按钮的ControlTemplate
<Style TargetType="Button" x:Key="newTemplate">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border x:Name="RootElement">
<!--Create the SolidColorBrush for the Background
as an object elemment and give it a name so
it can be referred to elsewhere in the control template.-->
<Border.Background>
<SolidColorBrush x:Name="BorderBrush" Color="Black"/>
</Border.Background>
<!--Create a border that has a different color by adding smaller grid.
The background of this grid is specificied by the button's Background
property.-->
<Grid Margin="4" Background="{TemplateBinding Background}">
<!--Use a ContentPresenter to display the Content of
the Button.-->
<ContentPresenter
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
Margin="4,5,4,4" />
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)
有关ControlTemplate的更多信息
数据模板是与控制模板相似的概念。它们为您提供了一种非常灵活而强大的解决方案,以替换诸如ListBox,ComboBox或ListView的控件中数据项的外观。WPF控件具有内置功能,可支持自定义数据表示。
DataTemplate的示例为
<!-- Without DataTemplate -->
<ListBox ItemsSource="{Binding}" />
<!-- With DataTemplate -->
<ListBox ItemsSource="{Binding}" BorderBrush="Transparent"
Grid.IsSharedSizeScope="True"
HorizontalContentAlignment="Stretch">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Margin="4">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" SharedSizeGroup="Key" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Name}" FontWeight="Bold" />
<TextBox Grid.Column="1" Text="{Binding Value }" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Run Code Online (Sandbox Code Playgroud)
有关数据模板和触发器的更多信息
您可以使用ItemTemplate指定数据对象的可视化。如果您的ItemsControl绑定到集合对象,并且没有使用DataTemplate提供特定的显示说明,则每个项目的结果UI都是基础集合中每个对象的字符串表示形式。
项目模板的示例为
<ListBox Margin="10" Name="lvDataBinding">
<ListBox.ItemTemplate>
<DataTemplate>
<WrapPanel>
<TextBlock Text="Name: " />
<TextBlock Text="{Binding Name}" FontWeight="Bold" />
<TextBlock Text=", " />
<TextBlock Text="Age: " />
<TextBlock Text="{Binding Age}" FontWeight="Bold" />
<TextBlock Text=" (" />
<TextBlock Text="{Binding Mail}" TextDecorations="Underline" Foreground="Blue" Cursor="Hand" />
<TextBlock Text=")" />
</WrapPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Run Code Online (Sandbox Code Playgroud)
在ItemsControl上设置ItemTemplate时,UI生成如下(以ListBox为例):
在内容生成期间,ItemsPanel向ItemContainerGenerator发起一个请求,以为每个数据项创建一个容器。对于ListBox,容器为ListBoxItem。生成器将调回ItemsControl以准备容器。
准备工作的一部分涉及将ListBox的ItemTemplate复制为ListBoxItem的ContentTemplate。
与所有ContentControl类型类似,ListBoxItem的ControlTemplate包含一个ContentPresenter。应用模板时,它将创建一个ContentPresenter,其ContentTemplate绑定到ListBoxItem的ContentTemplate。
最后,ContentPresenter将该ContentTemplate应用于自身,并创建UI。
如果定义了多个DataTemplate,并且要提供逻辑以编程方式选择和应用DataTemplate,请使用ItemTemplateSelector属性。
ItemsControl为可视化自定义提供了极大的灵活性,并提供了许多样式和模板属性。使用ItemContainerStyle属性或ItemContainerStyleSelector属性来设置样式以影响包含数据项的元素的外观。例如,对于ListBox,生成的容器是ListBoxItem控件。对于ComboBox,它们是ComboBoxItem控件。要影响项目的布局,请使用ItemsPanel属性。如果在控件上使用分组,则可以使用GroupStyle或GroupStyleSelector属性。
有关更多信息,请参见数据模板概述。
小智 2
ControlTemplaes 定义控件的“外观”和“行为”。默认情况下,按钮是矩形的。默认情况下,列表框具有白色背景。这些都是由Control的ControlTemple定义的。
DataTemplae 帮助控件管理它所保存的数据的布局。如果将用户列表添加到列表框中,并且您希望 UserName 显示在 UserPassword 之前,那么您将在 DataTemples 中定义它。DataTemples 被分配给ListBox 的ItemTemplate (4) 属性。
HierarchalDataTemplte 与 DataTemples 相同,只是它处理层次结构数据源。它通常与 TreeView 控件一起使用。
| 归档时间: |
|
| 查看次数: |
2361 次 |
| 最近记录: |