MSDN的解释:

提供一种根据数据对象和与该数据绑定的元素来选择数据模板 DataTemplate 的方法。

示例代码:

<Window x:Class="WpfApplication1.Window2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
Title="Window2" Height="300" Width="300" Loaded="Window2_OnLoaded">
<Window.Resources>
<DataTemplate x:Key="T1">
<TextBlock Text="{Binding}" Background="DarkBlue" Foreground="White" Padding="10,0"></TextBlock>
</DataTemplate> <DataTemplate x:Key="T2">
<TextBlock Text="{Binding}" Background="DarkCyan" Foreground="White" Padding="10,0"></TextBlock>
</DataTemplate> <local:CTemplateSelector x:Key="TS"></local:CTemplateSelector>
</Window.Resources>
<Grid>
<ListBox x:Name="Lbox" ItemTemplateSelector="{StaticResource TS}"></ListBox>
</Grid>
</Window>
public partial class Window2 : Window
{
public Window2()
{
InitializeComponent();
} private void Window2_OnLoaded(object sender, RoutedEventArgs e)
{
var source = new List<string>
{
"","","",""
}; this.Lbox.ItemsSource = source;
}
}
public class CTemplateSelector:DataTemplateSelector
{
public override DataTemplate SelectTemplate( object item, DependencyObject container )
{
if (item == null) throw new ArgumentNullException("item"); var element = container as FrameworkElement; if ( element != null )
{
var str = item.ToString();
int val; if (int.TryParse(str, out val))
{
if (val% == )
{
return element.FindResource("T1") as DataTemplate;
}
else
{
return element.FindResource( "T2" ) as DataTemplate;
}
}
} return base.SelectTemplate(item, container);
}
}

这个示例就是在一个 ListBox 控件中实现隔行换色的功能,当然也可以通过其他方法实现。

运行结果:

参考:

http://msdn.microsoft.com/zh-cn/library/system.windows.controls.datatemplateselector%28v=vs.110%29.aspx

WPF 中关于 DataTemplateSelector 类的应用的更多相关文章

  1. WPF中的Application类。

    原文:WPF中的Application类. Application对象用的名称空间是system.windows 1.手动创建Application对象步骤. 1.1).把项目中的App.Xaml文件 ...

  2. WPF中的常用类汇总:

    1.FrameworkElement: WPF中大部分的控件都可以转化成FrameworkElement,利用FrameworkElement属性获取相应的值: 2.WPF获取当前工作区域的宽度和高度 ...

  3. WPF中的MatrixTransform

    原文:WPF中的MatrixTransform WPF中的MatrixTransform                                                         ...

  4. MSDN 杂志:UI 前沿技术 - WPF 中的多点触控操作事件

    原文  MSDN 杂志:UI 前沿技术 - WPF 中的多点触控操作事件 UI 前沿技术 WPF 中的多点触控操作事件 Charles Petzold 下载代码示例 就在过去几年,多点触控还只是科幻电 ...

  5. 正确理解WPF中的TemplatedParent

    (注:Logical Tree中文称为逻辑树,Visual Tree中文称为可视化树或者视觉树,由于名称不是很统一,文中统一用英文名称代表两个概念,况且VisualTreeHelper和Logical ...

  6. 正确理解WPF中的TemplatedParent (转贴)

    http://blog.csdn.net/idebian/article/details/8761388 (注:Logical Tree中文称为逻辑树,Visual Tree中文称为可视化树或者视觉树 ...

  7. WPF中DataTemplateSelector的简单应用

    WPF中DataTemplateSelector的简单应用 DataTemplateSelector中文叫数据模板选择器,根据数据模型内的属性值选择不同的数据模板,多用于容器如listbox中,达到同 ...

  8. WPF中Image控件绑定到自定义类属性

    首先我们定义一个Student类,有ID,Name,Photo(保存图片路径). using System; using System.Collections.Generic; using Syste ...

  9. WPF中查找控件的扩展类

    在wpf中查找控件要用到VisualTreeHelper类,但这个类并没有按照名字查找控件的方法,于是搜索网络,整理出下面这个类,感觉用起来很是方便. 贴出来,供大家参考. /// <summa ...

随机推荐

  1. courator - create

    0. retry policy RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000,3); 1. client 1) recipes ...

  2. Android 接入X5WebView,让WebView加载更快;

    X5内核,微信和QQ浏览器都在用的WebView: 官网地址:https://x5.tencent.com,详细的信息进官网了解: 这是官方的宣传语: 1) 速度快:相比系统webview的网页打开速 ...

  3. Android悬浮框,在Service中打开悬浮窗;在Service中打开Dialog;

    文章介绍了如何在Service中显示悬浮框,在Service中弹出Dialog,在Service中做耗时的轮询操作: 背景需求: 公司的项目现在的逻辑是这样的:发送一个指令,然后3秒一次轮询去查询这个 ...

  4. linux&php:ubuntu安装php-7.2

    1.下载php源码,地址:http://www.php.net/downloads.php 这里下载的是tar.gz的包 2.解压安装 将安装包解压到/usr/local/php 安装C的编译工具 s ...

  5. python学习笔记目录

    人生苦短,我学python学习笔记目录: week1 python入门week2 python基础week3 python进阶week4 python模块week5 python高阶week6 数据结 ...

  6. 安全测试8_Web安全实战1(DVWA部署)

    1.渗透神器的打造(火狐浏览器的打造) Firebug HackBar(渗透插件) Tamper Data(抓包.截包.改包等功能) Proxy Switcher(代理设置) 2.PHP环境安装(ph ...

  7. java中六个时间类的使用和区别

    java.util.Date java.sql.Date   java.sql.Time   java.sql.Timestamp java.text.SimpleDateFormat java.ut ...

  8. 安装e(fx)clipse到Eclipse (JavaFX工具)

    转载自:https://www.yiibai.com/javafx/install-efxclipse-into-eclipse.html 想要在 Eclipse 中开发 JavaFX,那么可安装 J ...

  9. python学习笔记_week14

    Web框架本质 众所周知,对于所有的Web应用,本质上其实就是一个socket服务端,用户的浏览器其实就是一个socket客户端. s1 import socket def handle_reques ...

  10. zip和zipPartitions

    zip函数用于将两个RDD组合成Key/Value形式的RDD,这里默认两个RDD的partition数量以及元素数量都相同,否则会抛出异常. scala> val aa=sc.makeRDD( ...