在WPF中控制模板和数据模板之间的区别是什么?
当前回答
基本上,ControlTemplate描述了如何显示控件,而DataTemplate描述了如何显示数据。
例如:
标签是一个控件,它将包括一个控制模板,该控制模板表示标签应该在一些内容(数据模板或其他控件)周围使用边框显示。
一个客户类是数据,将显示使用一个DataTemplate,可以说显示客户类型为一个包含两个textblock的StackPanel,一个显示名称,另一个显示电话号码。这可能是有帮助的注意,所有的类都显示使用DataTemplates,你将通常使用默认模板,这是一个TextBlock的文本属性设置为对象的ToString方法的结果。
其他回答
ControlTemplate定义视觉外观,dattemplate替换数据项的视觉外观。
示例:我想显示一个按钮从矩形到圆形的形式=>控制模板。
如果控件有复杂的对象,它只调用并显示ToString(),使用DataTemplate,您可以获得各种成员并显示和更改数据对象的值。
控制模板-改变元素的外观。例如,按钮可以包含图像和文本
DataTemplate——使用元素表示底层数据。
以上所有的答案都很棒,但有一个关键的区别被忽略了。这有助于更好地决定何时使用什么。它是ItemTemplate属性:
DataTemplate is used for elements that provide ItemTemplate property for you to replace its items' content using DataTemplates you define previously according to bound data through a selector that you provide. But if your control does not provide this luxury for you then you still can use a ContentView that can display its content from predefined ControlTemplate. Interestingly, you can change the ControlTemplate property of your ContentView at runtime. One more thing to note that unlike controls with ItemTemplate property, you cannot have a TemplateSelector for this (ContentView) control. However, you still can create triggers to change the ControlTemplate at runtime.
基本上,ControlTemplate描述了如何显示控件,而DataTemplate描述了如何显示数据。
例如:
标签是一个控件,它将包括一个控制模板,该控制模板表示标签应该在一些内容(数据模板或其他控件)周围使用边框显示。
一个客户类是数据,将显示使用一个DataTemplate,可以说显示客户类型为一个包含两个textblock的StackPanel,一个显示名称,另一个显示电话号码。这可能是有帮助的注意,所有的类都显示使用DataTemplates,你将通常使用默认模板,这是一个TextBlock的文本属性设置为对象的ToString方法的结果。
Troels Larsen在MSDN论坛上有很好的解释
<Window x:Class="WpfApplication7.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Window.Resources> <DataTemplate x:Key="ButtonContentTemplate"> <StackPanel Orientation="Horizontal"> <Grid Height="8" Width="8"> <Path HorizontalAlignment="Stretch" Margin="0,0,1.8,1.8" VerticalAlignment="Stretch" Stretch="Fill" Stroke="#FF000000" Data="M0.5,5.7 L0.5,0.5 L5.7,0.5"/> <Path HorizontalAlignment="Stretch" Margin="2,3,0,0" VerticalAlignment="Stretch" Stretch="Fill" Stroke="#FFFFFFFF" Data="M3.2,7.5 L7.5,7.5 L7.5,3.5"/> <Path HorizontalAlignment="Stretch" Margin="1.2,1.4,0.7,0.7" VerticalAlignment="Stretch" Fill="#FFFFFFFF" Stretch="Fill" Stroke="#FF000000" Data="M2.5,2.5 L7.5,7.5"/> <Path HorizontalAlignment="Stretch" Margin="1.7,2.0,1,1" VerticalAlignment="Stretch" Stretch="Fill" Stroke="#FF000000" Data="M3,7.5 L7.5,7.5 L7.5,3.5"/> <Path HorizontalAlignment="Stretch" Margin="1,1,1,1" VerticalAlignment="Stretch" Stretch="Fill" Stroke="#FFFFFFFF" Data="M1.5,6.5 L1.5,1 L6.5,1.5"/> </Grid> <ContentPresenter Content="{Binding}"/> </StackPanel> </DataTemplate> <ControlTemplate TargetType="Button" x:Key="ButtonControlTemplate"> <Grid> <Ellipse Fill="{TemplateBinding Background}"/> <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/> </Grid> </ControlTemplate> </Window.Resources> <StackPanel> <Button Template="{StaticResource ButtonControlTemplate}" ContentTemplate="{StaticResource ButtonContentTemplate}" Content="1"/> <Button Template="{StaticResource ButtonControlTemplate}" ContentTemplate="{StaticResource ButtonContentTemplate}" Content="2"/> <Button Template="{StaticResource ButtonControlTemplate}" ContentTemplate="{StaticResource ButtonContentTemplate}" Content="3"/> </StackPanel> </Window> (Templates blatently stolen from http://msdn.microsoft.com/en-us/library/system.windows.controls.controltemplate.aspx and http://msdn.microsoft.com/en-us/library/system.windows.controls.contentcontrol.contenttemplate%28VS.95%29.aspx respectively) Anyway, the ControlTemplate decides how the Button itself looks, while the ContentTemplate decides how the Content of the button looks. So you could bind the content to one of you data classes and have it present itself however you wanted it.
推荐文章
- WPF数据网格底部空行
- 窗口vs页面vs用户控件的WPF导航?
- 在窗口上设置设计时间DataContext是一个编译器错误?
- 调用线程必须是STA,因为许多UI组件都要求这一点
- ContentControl和ContentPresenter有什么区别?
- WPF中控制模板和数据模板的区别
- 如何隐藏关闭按钮在WPF窗口?
- UI线程上的任务继续
- WPF数据绑定:我如何访问“父”数据上下文?
- 从System.Drawing.Bitmap中加载WPF BitmapImage
- 我应该使用什么MVVM框架?
- 在WPF中引入一个窗口到前面
- 窗口高度=“自动”未按预期工作
- 我如何得到一个动画gif在WPF工作?
- ListBox和ListView的区别是什么