
笔者个人认为使用DataTemplate时一定要先有数据然后在定义DataTemplate来显示这个数据以下是我遇到的三个场景1.容器控件使用来显示链表内单条数据容器控件有一个ItemTemplate属性是DataTemplate 类型[Bindable(true)] [CustomCategory(Content)] public DataTemplate ItemTemplate { get (DataTemplate) this.GetValue(ItemsControl.ItemTemplateProperty); set this.SetValue(ItemsControl.ItemTemplateProperty, (object) value); }例子telerik:RadTabControl.ItemTemplate DataTemplate TextBlock Text{Binding Name} / /DataTemplate /telerik:RadTabControl.ItemTemplateListBox ItemContainerStyle{StaticResource ActionItemStyle} ItemsSource{Binding OverallActions} ScrollViewer.HorizontalScrollBarVisibilityDisabled ListBox.ItemTemplate DataTemplate Border Margin3 BorderThickness1 CornerRadius5 Grid Grid.ColumnDefinitions ColumnDefinition Width* / ColumnDefinition WidthAuto / ColumnDefinition WidthAuto / /Grid.ColumnDefinitions StackPanel Grid.Column0 Margin10 TextBlock FontSize14 FontWeightBold Text{Binding ActionDescription} / /StackPanel TextBlock Grid.Column1 Margin10 HorizontalAlignmentCenter VerticalAlignmentCenter FontSize16 FontWeightBold Text{Binding ActionIndex} / !-- 操作按钮 -- StackPanel Grid.Column2 Margin10 OrientationHorizontal Button Width60 Height30 Margin0,0,5,0 Command{Binding DataContext.ExecuteActionHandleModelCommand, RelativeSource{RelativeSource AncestorType{x:Type ListBox}}} CommandParameter{Binding ActionIndex} Content执行 / /StackPanel /Grid /Border /DataTemplate /ListBox.ItemTemplate /ListBoxprivate OneBasedListActionHandleModel _overallActions new(); public OneBasedListActionHandleModel OverallActions { get _overallActions; set { this.RaiseAndSetIfChanged(ref _overallActions, value); } } public class ActionHandleModel : ReactiveObject { public int ActionIndex { get _actionIndex; set { this.RaiseAndSetIfChanged(ref _actionIndex, value); } } private int _actionIndex; public string ActionDescription { get _actionDescription; set { this.RaiseAndSetIfChanged(ref _actionDescription, value); } } private string _actionDescription; public bool IsCanRepeat { get _isCanRepeat; set { this.RaiseAndSetIfChanged(ref _isCanRepeat, value); } } private bool _isCanRepeat; public bool IsCanBackToPreviousStep { get _isCanBackToPreviousStep; set { this.RaiseAndSetIfChanged(ref _isCanBackToPreviousStep, value); } } private bool _isCanBackToPreviousStep; public SingleFlowExecutionState ExecutionState { get _executionState; set { this.RaiseAndSetIfChanged(ref _executionState, value); } } private SingleFlowExecutionState _executionState; }拓展在表格控件中使用DataTemplate来定义每一列样式时如果找不到绑定路径可以在旁边加上类型限制d:DataType的作用只是给设计器/智能提示用运行时绑定路径仍要自己写对。2.显示窗体的时候传入ViewModel指定模板来显示ViewDataTemplate DataType{x:Type local:AlarmHistoryDataViewModel} local:AlarmHistoryData / /DataTemplatelocal:AlarmHistoryData /实际上定义的是DataTemplate的VisualTree可视化树。因为DataTemplate 的继承链是这样的ContentPropertyAttribute 特性指定了属性名VisualTree那么在 DataTemplate 的 xaml内容中定义的元素实际上就是他的VisualTree属性有关链接ContentPropertyAttribute 类 (System.Windows.Markup) | Microsoft LearnXAML 语法指南 - Windows apps | Microsoft Learn3.显示某些数据的时候不想使用基本控件[SRCategory(AppearanceCategory)] public virtual DataTemplate ContentTemplate { get (DataTemplate) this.GetValue(RadTabControlBase.ContentTemplateProperty); set this.SetValue(RadTabControlBase.ContentTemplateProperty, (object) value); }telerik:RadTabControl ItemsSource{Binding Sections} SelectedIndex{Binding SelectSectionIndex} telerik:RadTabControl.ItemTemplate DataTemplate TextBlock Text{Binding Name} / /DataTemplate /telerik:RadTabControl.ItemTemplate telerik:RadTabControl.ContentTemplate DataTemplate telerik:RadGridView HorizontalContentAlignmentCenter VerticalContentAlignmentCenter AutoGenerateColumnsFalse CanUserGroupColumnsFalse CanUserReorderColumnsFalse CanUserResizeColumnsTrue CanUserSelectTrue CanUserSortColumnsFalse CanUserSortGroupsFalse EnableLostFocusSelectedStateFalse FrozenColumnsSplitterVisibilityCollapsed IsFilteringAllowedFalse IsHitTestVisibleTrue IsReadOnlyFalse IsTabStopFalse ItemsSource{Binding KeyValues, NotifyOnSourceUpdatedTrue} RowHeight29 RowIndicatorVisibilityCollapsed SelectionUnitFullRow ShowGroupPanelFalse ShowSearchPanelFalse telerik:RadGridView.Columns telerik:GridViewDataColumn Width* DataMemberBinding{Binding Key} Header程序枚举值 IsReadOnlyTrue / telerik:GridViewDataColumn Width2* DataMemberBinding{Binding Value} Header配置的名称 / /telerik:RadGridView.Columns /telerik:RadGridView /DataTemplate /telerik:RadTabControl.ContentTemplate /telerik:RadTabControlprivate ObservableCollectionIniSection _sections; public ObservableCollectionIniSection Sections { get _sections; set this.RaiseAndSetIfChanged(ref _sections, value); } public class IniSection { public string Name { get; set; } public ObservableCollectionIniKeyValue KeyValues { get; set; } new ObservableCollectionIniKeyValue(); } public class IniKeyValue { public string Key { get; set; } public string Value { get; set; } }这种有嵌套结构的数据使用基础控件不能显示使用TabControl 自定义数据模板比较方便每一个tab页就是一个IniSection对象在tab页里再定义一个表格显示KeyValues 属性