Window x:Class="模板选择器.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:local="clr-namespace:模板选择器"Title="MainWindow" Height="350" Width="525"><Window.Resources><local:MyDataTemplateSelector  x:Key="tmpdog"></local:MyDataTemplateSelector><DataTemplate x:Key="dta"><Border BorderThickness="2" BorderBrush="red" Background="GreenYellow"><StackPanel Orientation="Horizontal"><TextBlock Text="{Binding Name}"  Margin="10"></TextBlock><TextBlock Text="{Binding ID}" Margin="10"></TextBlock></StackPanel></Border></DataTemplate><DataTemplate x:Key="dtb" DataType="ListBox"><Border BorderThickness="1" BorderBrush="Blue" Background="LightBlue" Padding="5" ><StackPanel Orientation="Horizontal"><TextBlock Text="{Binding Name}"  Margin="10"></TextBlock><TextBlock Text="{Binding ID}" Margin="10"></TextBlock></StackPanel></Border></DataTemplate></Window.Resources><Grid><Grid.RowDefinitions><RowDefinition Height="*"></RowDefinition><RowDefinition Height="Auto"></RowDefinition></Grid.RowDefinitions><ListBox  Name="listbox" ItemTemplateSelector="{StaticResource tmpdog}"></ListBox><Button  Grid.Row="1" Width="75" Height="23" Click="Button_Click">确定</Button></Grid>
</Window>

  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;namespace 模板选择器
{/// <summary>/// MainWindow.xaml 的交互逻辑/// </summary>public partial class MainWindow : Window{public MainWindow(){InitializeComponent();InitData();listbox.ItemsSource = list;            }public List<Student> list { get; set; }public void InitData(){Random r = new Random();list = new List<Student>();for (int i = 0; i < 15; i++){list.Add(new Student{Name = "dog" + i,ID = r.Next(100)});}}int i = 0;private void Button_Click(object sender, RoutedEventArgs e){if (i%2==0){listbox.SetResourceReference(ListBox.ItemTemplateProperty, "dta");}else{listbox.SetResourceReference(ListBox.ItemTemplateProperty, "dtb");}i++;}}public class Student{public int ID{set;get;}public string Name{set;get;}}public class MyDataTemplateSelector : DataTemplateSelector{public override DataTemplate SelectTemplate(object item, DependencyObject container){var fe = container as FrameworkElement;var obj = item as Student;DataTemplate dt = null;if (obj != null && fe != null){if (obj.ID > 30){dt = fe.FindResource("dta") as DataTemplate;}                    else{dt = fe.FindResource("dtb") as DataTemplate;}                    }return dt;}}
}

 

转载于:https://www.cnblogs.com/dog2016/p/8038188.html

WPF 动态更换模板相关推荐

  1. 继续聊WPF——动态数据模板

    我为啥称之为"动态数据模板"?先看看下面的截图,今天,我们就是要实现这种功能. 大概是这样的,我们定义的DataTemplate是通过触发器动态应用到 ComboBoxItem 上 ...

  2. WPF 动态更换图片路径

    wpf中动态修改图片路径,其实很简单,有个小伙伴有疑问,绑定了source,为什么不能显示图片呢... 通过绑定,修改图片路径,动态显示图片,效果如下: 图片支持本地路径和网络路径,下面就来看看如何做 ...

  3. 你知道WPF这三大模板实例运用吗?

    1.介绍 对于Windows桌面端应用开发来讲,WPF以其界面渲染的特殊性,灵活的界面布局而让人津津乐道,因为它能为用户提供更好的交互体验.如何利用WPF开发出让人赏心悦目的界面与功能呢?这里不仅仅只 ...

  4. MOGRT替换视频,图像,照片及LOGO?如何替换PR动态图形模板中的图片视频素材

    在PR2020(Premiere Pro 2020及较早的版本)中使用PR动态图形模板MOGRT时,替换图片或视频需要使用AE软件,且流程较为复杂,这对于很多PR新手来说,是非常不友好的! 不负期待! ...

  5. 《深入浅出WPF》笔记——模板篇

    原文:<深入浅出WPF>笔记--模板篇 我们通常说的模板是用来参照的,同样在WPF中,模板是用来作为制作控件的参照. 一.认识模板 1.1WPF菜鸟看模板 前面的记录有提过,控件主要是算法 ...

  6. (0042) iOS 开发之iOS10.3新特性:如何动态更换APP图标

    iOS新特性: iOS10.3教你如何动态更换APP图标? 1.iOS 10.3 开放了更换 app 图标的 API,核心方法是下面这个: [[UIApplication sharedApplicat ...

  7. vue 修改模板{{}}标签_详解Vue 动态添加模板的几种方法

    以下方法只适用于 Vue1.0 版本,推荐系数由高到低排列. 通常我们会在组件里的 template 属性定义模板,或者是在*.vue文件里的 template 标签里写模板.但是有时候会需要动态生成 ...

  8. pr图形模板预设怎么使用_技术丨PR怎样安装动态图形模板?Pr mac版怎样调用Mogrt预设?Adobe Premiere导入MOGRTs预设完整教程...

    Mogrts是一动态图形模板是一种可在 After Effects 或 Premiere Pro 中创建的文件类型 (.mogrt).可以帮助用户轻松使用PR编辑简单特效动画.下面是小编为大家整理的p ...

  9. vue动态设置文字布局方式_详解Vue动态添加模板的几种方法

    动态添加模板需要收集原始数据的页面,这个时候我们需要很多原始数据收集模板,下面给大家详解Vue 动态添加模板的几种方法,希望对你学习这方面知识有所帮助. 通常我们会在组件里的 template 属性定 ...

最新文章

  1. python读取文件
  2. python @property
  3. 一个例子理解什么是RPC?
  4. 二叉树的基本操作_二叉树的遍历
  5. python性能优化之函数执行时间分析_python性能优化之函数执行时间分析
  6. 性能测试入门(二)转:JMeter基础之一 一个简单的性能测试
  7. C# - dynamic 特性
  8. 通过标题区别自己的发帖
  9. C语言实用算法系列之学生管理系统_单向链表内操作_选择排序
  10. 针对Parse error: syntax error, unexpected T_PUBLIC in D:报错提醒
  11. 解决vuepress-theme-reco中文标签跳转返回404问题
  12. 定义一个时间类Time,能提供和设置由时、分、秒组成的时间,并编出应用程序,定义时间对象,设置时间,输出该对象提供的时间。
  13. P1293 班级聚会洛谷c++题解
  14. Java面试通关要点汇总集(基础篇之基本功,非原作者)
  15. proteus软件闪退与许可证到期问题解决办法
  16. MarkDown的简介
  17. Origin2018怎么画出向上向下的柱状图
  18. 一款程序员值得拥有的机械键盘:Keychron K3
  19. 利用reportviewer与C#生成报表
  20. 21-12-05周总结

热门文章

  1. 《机器学习》赵卫东学习笔记 第13章推荐系统(课后习题及答案)
  2. M4a文件解析(一)---某些播放器不能播放m4a(如炬芯播放器)
  3. python多线程简单示例
  4. 医院应急系统的需求与设计
  5. R语言倾向性评分:加权
  6. 愤怒的小鸟重制版 Angry Birds Reloaded for Mac(经典的休闲游戏)
  7. 网络模拟http数据包
  8. 手机录音amr怎么转mp3?三种方法直接转换!
  9. 嵌入式linux golang,golang嵌入式开发
  10. 文献阅读-ICRA2020-从单眼内窥镜图像中对手术机器人器械的柄姿势估计