参考: https://wpf.2000things.com/2017/04/06/1204-using-a-datatrigger-to-change-content-in-a-contentpresenter/

<Window.Resources>
<DataTemplate x:Key="DefaultContent">
<StackPanel>
<TextBlock Margin="10" Text="Some default content here.."/>
<TextBlock Margin="10" Text="Maybe show progress for operation"/>
</StackPanel>
</DataTemplate> <DataTemplate x:Key="AllDoneContent">
<StackPanel>
<TextBlock Margin="10" Text="** This is the ALL DONE content..."
Foreground="Green"/>
<TextBlock Margin="10" Text="Put anything you like here"/>
<Button Margin="10" Content="Click Me" HorizontalAlignment="Left"/>
</StackPanel>
</DataTemplate> <Style x:Key="MyContentStyle" TargetType="ContentPresenter">
<Setter Property="ContentTemplate" Value="{StaticResource DefaultContent}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding JobDone}" Value="True">
<Setter Property="ContentTemplate" Value="{StaticResource AllDoneContent}"/>
</DataTrigger>
</Style.Triggers>
</Style> </Window.Resources> <Grid Margin="10">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions> <ContentPresenter Grid.Row="0" Style="{StaticResource MyContentStyle}" Content="{Binding}"/> <Separator Grid.Row="1"/> <CheckBox Grid.Row="2" Margin="10" Content="Mark job done" IsChecked="{Binding JobDone}"/>
</Grid>

ContentControl通过DataTrigger进行切换模板时,原来绑定的模板会引发DataContext事件

使用ContentPresenter,不使用ContentControl的更多相关文章

  1. 重新想象 Windows 8 Store Apps (3) - 控件之内容控件: ToolTip, Frame, AppBar, ContentControl, ContentPresenter; 容器控件: Border, Viewbox, Popup

    原文:重新想象 Windows 8 Store Apps (3) - 控件之内容控件: ToolTip, Frame, AppBar, ContentControl, ContentPresenter ...

  2. ContentControl和ContentPresenter的应用

    1:wpf中,所有的内容控件都继承自“ContentControl” ,所以我们可以直接应用“ContentControl”自定义我们“需要的”内容控件. 2:ContentControl具有Cont ...

  3. WPF的Presenter(ContentPresenter)(转)

    这是2年前写了一篇文章 http://www.cnblogs.com/Clingingboy/archive/2008/07/03/wpfcustomcontrolpart-1.html 我们先来看M ...

  4. 正确理解ContentPresenter

    下图显示继承关系: ContentControl:Control (在Control類並沒有Content屬性, 所以在這之上再寫了一個ContentControl, 使控件有Content屬性可以顯 ...

  5. ContentPresenter

    这是2年前写了一篇文章 http://www.cnblogs.com/Clingingboy/archive/2008/07/03/wpfcustomcontrolpart-1.html 我们先来看M ...

  6. WPF:为什么使用ContentPresenter.ContentSource而不是Content属性?

    因为ContentPresenter.ContentSource比Content属性加一个TemplateBinding看起来更方便?不仅仅是这些,实际上如果用ContentSource的话,Cont ...

  7. WPF的Presenter(ContentPresenter)

    WPF的Presenter(ContentPresenter) 2010-12-20 14:34 by Clingingboy, 10619 阅读, 3 评论, 收藏, 编辑 这是2年前写了一篇文章 ...

  8. ContentPresenter理解

    这是2年前写了一篇文章 http://www.cnblogs.com/Clingingboy/archive/2008/07/03/wpfcustomcontrolpart-1.html 我们先来看M ...

  9. [转]WPF的Presenter(ContentPresenter)

    这是2年前写了一篇文章 http://www.cnblogs.com/Clingingboy/archive/2008/07/03/wpfcustomcontrolpart-1.html 我们先来看M ...

  10. WPF模板

    WPF的中模板有三种:ControlTemplate.ItemsPanelTemplate.DataTemplate,他们继承抽象类FrameworkTemplate,下面是它们的继承关系: Wind ...

随机推荐

  1. 前端调试利器---nproxy

    前言:习惯了在windows环境中使用Fiddler的童鞋们,是不是感觉它的网络重定向功能很酷,Fiddler能按照你设置的规制捕获网络请求,再指向本地文件,如拦截你的js文件到本地,就能很快的调试线 ...

  2. 用Python将word文件转换成html(转)

    用Python将word文件转换成html   序 最近公司一个客户大大购买了一堆医疗健康方面的科普文章,希望能放到我们正在开发的健康档案管理软件上.客户大大说,要智能推送!要掌握节奏!要深度学习!要 ...

  3. MySQL修改redo_log_size

    MySQL5.5 步骤如下: 1. set global innodb_fast_shutdown = 0; 2. mysqladmin shutdown 3. 修改my.cnf innodb_log ...

  4. jena读取和解析本体文件

    使用jena开发本体应用程序时,首先需要对我们利用本体构建工具,如protege等,构建的本体文件,如owl.rdf等读取并解析得到本体模型.下面给出相应的代码,不对的地方请指正. (基于jena 2 ...

  5. 好一个Time_Wait状态(TCP/IP)

    首先简单介绍一下Time_Wait是个什么鬼: 在TCP/IP协议中,我们都知道有三次握手四次挥手的过程,先来一个简单的图: 各个状态和基本的过程想必了解过TCP/IP协议的人都清楚,本次介绍的主题只 ...

  6. java算法 第七届 蓝桥杯B组(题+答案) 1.煤球数目

    1.煤球数目  (结果填空) 有一堆煤球,堆成三角棱锥形.具体:第一层放1个,第二层3个(排列成三角形),第三层6个(排列成三角形),第四层10个(排列成三角形),....如果一共有100层,共有多少 ...

  7. Spark之 使用SparkSql操作mysql和DataFrame的Scala实现

    通过读取文件转换成DataFrame数据写入到mysql中 package com.zy.sparksql import java.util.Properties import org.apache. ...

  8. centos7 安装VMware Tools 遇到的一系列问题的解决方案

    先部署源:http://www.cnblogs.com/jiu0821/p/8074463.html VMware Tools安装方法参考:http://www.cnblogs.com/jiu0821 ...

  9. TP3.1 一对多模型关联

    TP3.1.3 的一对多的模型关联 老需求 --- 一个用户多个文章,查看这些文章   HasMany 首先定义Model 模型名字叫UserMode.class.php class UserMode ...

  10. c++ 享元模式(flyweight)

    举个围棋的例子,围棋的棋盘共有361格,即可放361个棋子.现在要实现一个围棋程 序,该怎么办呢?首先要考虑的是棋子棋盘的实现,可以定义一个棋子的类,成员变量包括棋子的颜色.形状.位置等信息,另外再定 ...