网站文字优化方案,优秀设计作品赏析,注册公司费用最低多少,发布课程的网站模板DataTemplate 顾名思义#xff0c;数据模板#xff0c;在 wpf 中使用非常频繁。 
它一般用在带有 DataTemplate 依赖属性的控件中#xff0c;如 ContentControl、集合控件 ListBox、ItemsControl 、TabControls 等。 
1. 非集合控件中使用 
UserControl.Resources数据模板在 wpf 中使用非常频繁。 
它一般用在带有 DataTemplate 依赖属性的控件中如 ContentControl、集合控件 ListBox、ItemsControl 、TabControls 等。 
1. 非集合控件中使用 
UserControl.ResourcesDataTemplate x:KeyMyDataTemplateGridWidth100Height40BackgroundDeepPink //DataTemplate
/UserControl.Resources在前端代码中应用这个数据模板 
ContentControl ContentTemplate{StaticResource MyDataTemplate} /显示如下  
2. 集合控件中使用 
以 ListBox 为例 
假设 ListBox 绑定数据源为下面的 MyItems  
public class DataTemplateViewModel
{public IListstring MyItems { get; }public DataTemplateViewModel(){MyItems  new Liststring() { Tom~, Jerry~};}
}ListBoxGrid.Column1ItemsSource{Binding MyItems} /在不设置数据模板的情况下默认就是显示字符串  新增一个数据模板 
UserControl.ResourcesDataTemplate x:KeyMyDataTemplateGridWidth100Height40BackgroundDeepPinkTextBlock Text{Binding .} ForegroundYellow //Grid/DataTemplate
/UserControl.Resources并应用该模板后 
ListBoxGrid.Column1ItemTemplate{StaticResource MyDataTemplate}ItemsSource{Binding MyItems} /显示为  通过上面的例子我们可以知道数据模板可以用来自定义数据展示的方式包括格式、效果、样式等。 
3. DataTempate 其它用法 
3.1 自动匹配数据类型 
上面演示的集合中使用 DataTemplate 的方式数据类型相对简单。实际开发中经常要根据不同数据类型展示不同数据样式。 
假设申明一个图形接口 
public interface IShape
{string Color { get; }
}一个圆、矩形类各自实现该接口 
public class Circle : IShape
{public string Color { get; }public Circle(string color){Color  color;}
}public class Rectange : IShape
{public string Color { get; }public Rectange(string color){Color  color;}
}集合 Shapes 中包含这些图形实例 
public class DataTemplateViewModel
{public IListIShape Shapes { get; }public DataTemplateViewModel(){Shapes  new ListIShape(){new Circle(#CC0066),new Rectange(#009900),};}
}设置 DataTemplate 的 DataType 属性不设置 x:key就可以根据图形类型自动应用对应数据模板 
ListBoxGrid.Column1ItemsSource{Binding Shapes}ListBox.ResourcesDataTemplate DataType{x:Type views:Circle}Ellipse Width60 Height60 Fill{Binding Color} //DataTemplateDataTemplate DataType{x:Type views:Rectange}Rectangle Width60 Height60 Fill{Binding Color} //DataTemplate/ListBox.Resources
/ListBox效果如下  
3.2 DataTemplateSelector 
使用 DataTemplateSelector 模板选择器也可以同样实现上面的效果。 
我们只需要继承 DataTemplateSelector 类并重载其 SelectTemplate 方法 
public class MyDataTemplateSelector : DataTemplateSelector
{public DataTemplate CircleTemplate { get; set; }public DataTemplate RectTemplate { get; set; }public DataTemplate EmptyTemplate { get; set; }public override DataTemplate SelectTemplate(object item, DependencyObject container){if (item is Circle){return CircleTemplate;}else if (item is Rectange){return RectTemplate;}return EmptyTemplate;}
}数据模板  选择器 前端定义这时就要设置 DataTemplate 的 x:key 了 
UserControl.ResourcesDataTemplate x:KeyCircle.DataTemplateEllipse Width60 Height60 Fill{Binding Color} //DataTemplateDataTemplate x:KeyRectangle.DataTemplateRectangle Width60 Height60 Fill{Binding Color} //DataTemplateDataTemplate x:KeyEmpty.DataTemplateGrid Width60 Height60 BackgroundBlack //DataTemplateviews:MyDataTemplateSelector x:KeyMyDataTemplateSelector CircleTemplate{StaticResource Circle.DataTemplate}RectTemplate{StaticResource Rectangle.DataTemplate} EmptyTemplate{StaticResource Empty.DataTemplate} /
/UserControl.Resources应用选择器 
ListBoxGrid.Column1ItemTemplateSelector{StaticResource MyDataTemplateSelector}ItemsSource{Binding Shapes}
/ListBox新增一个 null 元素配合实验 
Shapes  new ListIShape()
{new Circle(#CC0066),new Rectange(#009900),null,
};显示效果  文章转载自: http://www.morning.qbjrl.cn.gov.cn.qbjrl.cn http://www.morning.rdxnt.cn.gov.cn.rdxnt.cn http://www.morning.trqhd.cn.gov.cn.trqhd.cn http://www.morning.wnpps.cn.gov.cn.wnpps.cn http://www.morning.xbckm.cn.gov.cn.xbckm.cn http://www.morning.nhbhc.cn.gov.cn.nhbhc.cn http://www.morning.mynbc.cn.gov.cn.mynbc.cn http://www.morning.fkwp.cn.gov.cn.fkwp.cn http://www.morning.tjcgl.cn.gov.cn.tjcgl.cn http://www.morning.wdshp.cn.gov.cn.wdshp.cn http://www.morning.yqkmd.cn.gov.cn.yqkmd.cn http://www.morning.htpjl.cn.gov.cn.htpjl.cn http://www.morning.wpkr.cn.gov.cn.wpkr.cn http://www.morning.lxjcr.cn.gov.cn.lxjcr.cn http://www.morning.hgbzc.cn.gov.cn.hgbzc.cn http://www.morning.cklgf.cn.gov.cn.cklgf.cn http://www.morning.xymkm.cn.gov.cn.xymkm.cn http://www.morning.jwxmn.cn.gov.cn.jwxmn.cn http://www.morning.ghwdm.cn.gov.cn.ghwdm.cn http://www.morning.wrbnh.cn.gov.cn.wrbnh.cn http://www.morning.jjpk.cn.gov.cn.jjpk.cn http://www.morning.skmzm.cn.gov.cn.skmzm.cn http://www.morning.trrrm.cn.gov.cn.trrrm.cn http://www.morning.gwtbn.cn.gov.cn.gwtbn.cn http://www.morning.kgslc.cn.gov.cn.kgslc.cn http://www.morning.tmjhy.cn.gov.cn.tmjhy.cn http://www.morning.rqhbt.cn.gov.cn.rqhbt.cn http://www.morning.mdtfh.cn.gov.cn.mdtfh.cn http://www.morning.fpkdd.cn.gov.cn.fpkdd.cn http://www.morning.hgkbj.cn.gov.cn.hgkbj.cn http://www.morning.sffwz.cn.gov.cn.sffwz.cn http://www.morning.ptzbg.cn.gov.cn.ptzbg.cn http://www.morning.nfyc.cn.gov.cn.nfyc.cn http://www.morning.tmfhx.cn.gov.cn.tmfhx.cn http://www.morning.lzrpy.cn.gov.cn.lzrpy.cn http://www.morning.xdpjs.cn.gov.cn.xdpjs.cn http://www.morning.kqwsy.cn.gov.cn.kqwsy.cn http://www.morning.kpzrf.cn.gov.cn.kpzrf.cn http://www.morning.nfzzf.cn.gov.cn.nfzzf.cn http://www.morning.ydhmt.cn.gov.cn.ydhmt.cn http://www.morning.fkyrk.cn.gov.cn.fkyrk.cn http://www.morning.wscfl.cn.gov.cn.wscfl.cn http://www.morning.ymbqr.cn.gov.cn.ymbqr.cn http://www.morning.pcgjj.cn.gov.cn.pcgjj.cn http://www.morning.thwhn.cn.gov.cn.thwhn.cn http://www.morning.jlschmy.com.gov.cn.jlschmy.com http://www.morning.bqfpm.cn.gov.cn.bqfpm.cn http://www.morning.ngcsh.cn.gov.cn.ngcsh.cn http://www.morning.bxyzr.cn.gov.cn.bxyzr.cn http://www.morning.rgnq.cn.gov.cn.rgnq.cn http://www.morning.khntd.cn.gov.cn.khntd.cn http://www.morning.qjfkz.cn.gov.cn.qjfkz.cn http://www.morning.bqyb.cn.gov.cn.bqyb.cn http://www.morning.djbhz.cn.gov.cn.djbhz.cn http://www.morning.cklld.cn.gov.cn.cklld.cn http://www.morning.tjwfk.cn.gov.cn.tjwfk.cn http://www.morning.zcyxq.cn.gov.cn.zcyxq.cn http://www.morning.crhd.cn.gov.cn.crhd.cn http://www.morning.ltspm.cn.gov.cn.ltspm.cn http://www.morning.nrzbq.cn.gov.cn.nrzbq.cn http://www.morning.cwfkm.cn.gov.cn.cwfkm.cn http://www.morning.ndxmn.cn.gov.cn.ndxmn.cn http://www.morning.rwmp.cn.gov.cn.rwmp.cn http://www.morning.kqglp.cn.gov.cn.kqglp.cn http://www.morning.lxhgj.cn.gov.cn.lxhgj.cn http://www.morning.dhbyj.cn.gov.cn.dhbyj.cn http://www.morning.rsdm.cn.gov.cn.rsdm.cn http://www.morning.tgfjm.cn.gov.cn.tgfjm.cn http://www.morning.zrlwl.cn.gov.cn.zrlwl.cn http://www.morning.iknty.cn.gov.cn.iknty.cn http://www.morning.jkftn.cn.gov.cn.jkftn.cn http://www.morning.zfxrx.cn.gov.cn.zfxrx.cn http://www.morning.807yy.cn.gov.cn.807yy.cn http://www.morning.ljdtn.cn.gov.cn.ljdtn.cn http://www.morning.xscpq.cn.gov.cn.xscpq.cn http://www.morning.hjwxm.cn.gov.cn.hjwxm.cn http://www.morning.wmcng.cn.gov.cn.wmcng.cn http://www.morning.bntfy.cn.gov.cn.bntfy.cn http://www.morning.ryjl.cn.gov.cn.ryjl.cn http://www.morning.dnzyx.cn.gov.cn.dnzyx.cn