WPF ContentControl
参考代码:https://github.com/kuku2504/SummarySolution
定义
命名空间:
System.Windows.Controls
程序集:
PresentationFramework.dll
表示包含一段任意类型内容的控件。
// 摘要:// 表示一段单独的任何类型的内容的控件。[ContentProperty("Content")][DefaultProperty("Content")][Localizability(LocalizationCategory.None, Readability = Readability.Unreadable)]public class ContentControl : Control, IAddChild
继承
示例
备注:尽管该示例的可扩展应用程序标记语言(XAML)版本可以使用<Button.Content>每个按钮内容周围的标记,但没有必要。
<!--Create a Button with a string as its content.-->
<Button>This is string content of a Button</Button><!--Create a Button with a DateTime object as its content.-->
<Button xmlns:sys="clr-namespace:System;assembly=mscorlib"><sys:DateTime>2004/3/4 13:6:55</sys:DateTime>
</Button><!--Create a Button with a single UIElement as its content.-->
<Button><Rectangle Height="40" Width="40" Fill="Blue"/>
</Button><!--Create a Button with a panel that contains multiple objects
as its content.-->
<Button><StackPanel><Ellipse Height="40" Width="40" Fill="Blue"/><TextBlock TextAlignment="Center">Button</TextBlock></StackPanel>
</Button>
下面的示例演示如何为控件创建样式ContentControl,以便控件具有增强的视觉外观
<Style x:Key="ContentCtrl" TargetType="{x:Type ContentControl}"><Setter Property="Background" Value="Red"/><Setter Property="Foreground" Value="Green"/><Setter Property="FontSize" Value="20"/><Setter Property="FontWeight" Value="Bold"/><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="{x:Type ContentControl}"><Grid><!--Keep the Ellipse a circle when ContentControl.Width is set.--><Ellipse Width="{TemplateBinding Width}"Height="{TemplateBinding Width}"Fill="{TemplateBinding Background}"/><ContentPresenter VerticalAlignment="Center"HorizontalAlignment="Center"/></Grid></ControlTemplate></Setter.Value></Setter>
</Style>
<ContentControl Width="75" Style="{StaticResource ContentCtrl}"Content="Hello"/>
备注:
可以 ContentControl 包含任何类型的公共语言运行时对象 (,例如字符串或 DateTime 对象) 或 UIElement 对象 (,例如或 Rectangle Panel) 。 这使你可以向控件(如和CheckBox)Button添加丰富的内容。
ContentControl 具有有限的默认样式。 如果要增强控件的外观,可以创建新的 DataTemplate控件
官网详细讲解如下链接:
https://learn.microsoft.com/zh-cn/dotnet/api/system.windows.controls.contentcontrol?view=windowsdesktop-6.0
ContentControl 动态绑定UserControl控件
1. 步骤一
把SchoolInfoView的视图,绑定到ContentControl中
// 摘要:// 获取或设置的内容 System.Windows.Controls.ContentControl。// 返回结果:// 包含控件的内容的对象。 默认值为 null。[Bindable(true)][CustomCategoryAttribute("Content")]public object Content { get; set; }
把SchoolInfoView作为一个内容控件放在ContentControl的属性Content中
在父窗口对应的ViewModel中
2. 步骤二
以上两个步骤,就可以把View绑定到ContentControl控件的Content中了
运行结果如下:
注:
//// 摘要:// 获取或设置元素参与数据绑定时的数据上下文。//// 返回结果:// 要用作数据上下文的对象。[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)][Localizability(LocalizationCategory.NeverLocalize)]public object DataContext { get; set; }
参考代码:https://github.com/kuku2504/SummarySolution
WPF ContentControl相关推荐
- WPF开发人员必读:WPF控件测试台
介绍 WpfControlTestbench帮助您为您的控件或您想要调查其行为的任何控件编写快速复杂的测试窗口.只需十几行XAML即可创建以下Window内容: 它在左下角显示你要测试的控件,在Win ...
- WPF 控件【U】UserControl(一) UserControl、ContentControl、Page的区别,及它们的使用方法
一.简介 本文将介绍用户控件UserControl与Page的区别,及两者的使用方法. 部分参考: https://www.cnblogs.com/lelehellow/p/6026766.html( ...
- [WPF] UserControl vs CustomControl
原文:[WPF] UserControl vs CustomControl 介绍 WPF中有两种控件:UserControl和CustomControl,但是这两者有什么区别呢?这篇博客中将介绍两者之 ...
- WPF and Silverlight 学习笔记(十):WPF控件模型
WPF对控件其类型的继承方式如下 (其中绿色表示的类是抽象类,蓝色表示的类是非抽象类) 控件内容模型 System.Windows.Controls.Control类:表示 用户界面 (UI ...
- WPF QuickStart系列之样式和模板(Style and Template)
原文:WPF QuickStart系列之样式和模板(Style and Template) 在WPF桌面程序中,当我们想构建一个统一的UI表现时(在不同操作系统下,显示效果一致),此时我们就需要使用到 ...
- 【WPF】用CustomControl打造WPF版的Marquee
控件代码已经更新,支持上下左右四个方向.VS2010工程 /Files/RMay/WPF_Marquee/WpfMarquee.zip 我们知道在html中有一个marquee标签,可以很方便的实现文 ...
- [书目20130216]深入浅出WPF
目录 写作缘起 WPF之What & Why 致谢 第一部分 深入浅出话XAML 第1章 XAML概览 2 1.1 XAML是什么 2 1.2 XAML的优点 3 第2章 从零起步认 ...
- WPF入门知识(学习)
WPF基础知识 快速学习绝不是从零学起的,良好的基础是快速入手的关键,下面先为大家摞列以下自己总结的学习WPF的几点基础知识: 1) C#基础语法知识(或者其他.NET支持的语言):这个是当然的了,虽 ...
- wpf控件开发基础(1)
从现在开始,我将尝试写有关wpf控件开发相关的知识,把文章这对我来说很难,所以这个系列的文章在时间跨度上可能会拖的比较长.我希望我介绍是比较详细的,而不仅仅是一个简单的控件开发流程.我是一个真正的We ...
- WPF下可编辑Header的Tab控件实现
介绍 有这样一个需求,当用户双击Tab控件Header区域时, 希望可以直接编辑.对于WPF控件,提供一个ControlTemplate在加上一些Trigger就可以实现.效果如下: 代码 首先,我们 ...
最新文章
- 「深度学习知识体系总结(2021版)」开放下载了!
- java nio 断开连接_浅尝Java NIO与Tomcat连接调优
- js原生设计模式——2面向对象编程之继承—new+call(this)组合式继承
- 四元数左乘右乘_四元数、欧拉角学习笔记个人理解
- 【Scrapy】Unsupported major.minor version 52.0 [duplicate]
- .Net Core控制台amp;EFCore连接Mysql
- CSS中可以通过哪些属性定义,使得一个DOM元素不显示在浏览器可视范围内?
- L1-004. 计算摄氏温度-PAT团体程序设计天梯赛GPLT
- php采集今日头条,用php蓝天采集器抓取今日头条ajax的文章内容
- 内功修炼之lodash——chunk、zip、groupBy、invokeMap方法
- 批量注册阿里云备案域名
- SQL学习笔记6-决胜秋招
- 金色新版萝卜影视系统源码+影视系统APP源码
- LLVM IR转CFG
- 非因推荐 | 肿瘤微环境研究大剖析——肿瘤相关巨噬细胞
- linux下的挂载点和分区是什么关系(详解挂载点)
- WriteProfileInt
- MATLAB利用QQ图检验总体是否服从多维正态分布
- 友盟iOS微信登陆为什么没有回调
- spring依赖注入的4种方式