简介

"用户控件"继承自UserControl,而UserControl继承自ContentControl,也就是内容控件
UserControl和Window是一个层次上的,都有xaml和cs文件

流程

创建用户控件

写好用户控件

<UserControl x:Class="WpfDemo.UserControlDemo.OwnUserControl"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="*"></RowDefinition> <RowDefinition Height="*"></RowDefinition> </Grid.RowDefinitions> <Label Content="这是一个用户控件"></Label> <Slider Minimum="0" Maximum="100" Grid.Row="1"></Slider> </Grid> </UserControl> 

用户控件也可以套用用户控件,组成更复杂的界面

<UserControl x:Class="WpfDemo.UserControlDemo.UseUserControlUserControl"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:userControlDemo="clr-namespace:WpfDemo.UserControlDemo" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="*"></RowDefinition> <RowDefinition Height="2*"></RowDefinition> <RowDefinition Height="*"></RowDefinition> <RowDefinition Height="*"></RowDefinition> </Grid.RowDefinitions> <Label Content="这是另一个用户控件,在这里使用了OwnUserControl用户控件"></Label> <userControlDemo:OwnUserControl Grid.Row="1"></userControlDemo:OwnUserControl> <Label Grid.Row="2" Content="这个用户控件在OwnUserControl的基础上再添加一个Slider"></Label> <Slider Grid.Row="3" Minimum="0" Maximum="200" ></Slider> </Grid> </UserControl> 

窗体引用用户控件

<Window x:Class="WpfDemo.UserControlDemo.UseUserControlWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:userControlDemo="clr-namespace:WpfDemo.UserControlDemo" mc:Ignorable="d" Title="UseUserControlWindow" Height="600" Width="400"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="0.2*"></RowDefinition> <RowDefinition Height="*"></RowDefinition> <RowDefinition Height="0.2*"></RowDefinition> <RowDefinition Height="*"></RowDefinition> </Grid.RowDefinitions> <Label Content="使用OwnUserControl用户控件"></Label> <userControlDemo:OwnUserControl Grid.Row="1"></userControlDemo:OwnUserControl> <Label Grid.Row="2" Content="使用UseUserControlUserControl用户控件"></Label> <userControlDemo:UseUserControlUserControl Grid.Row="3"></userControlDemo:UseUserControlUserControl> </Grid> </Window> 

示例代码

https://github.com/zLulus/NotePractice/tree/dev3/WPF/WpfDemo/UserControlDemo

转载于:https://www.cnblogs.com/Lulus/p/8151405.html

用户控件(UserControl)相关推荐

  1. ASP.NET中实现复用代码自定义用户控件UserControl的使用

    场景 ASP.NET中新建Web网站并部署到IIS上(详细图文教程): https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/107199 ...

  2. C#中,用户控件UserControl里面用Panl加载UserControl,并实现利用委托互相传值

    用户控件主窗体结构:左侧树形菜单,右侧Panl: 根据点击的菜单节点,panl里面选择性加载某一个子窗体用户控件,并传值给子窗体: 反之,在子窗体进行相应的操作之后,传值给主窗体,触发主窗体的刷新. ...

  3. 【WPF】Xaml用户控件(Usercontrol)绑定属性/事件

    Usercontrol可以将已有的控件重新组合为新的控件,本文将会说明如何实现前台代码设置Usercontol的属性以及事件. 首先,我们先新建一个Usercontrol,命名为:UC UC中,有一个 ...

  4. 关于ASP.Net页面上用户控件相互调用的方法 .

    引至:http://blog.csdn.net/aqgsh/article/details/6941466 最近做一个项目,在首面上有几个用户控件,其中一个是显示当前登录的用户名,位于名为PageHe ...

  5. UWP WP8.1 依赖属性和用户控件 依赖属性简单使用 uwp添加UserControl

    上面说 附加属性.这章节说依赖属性. 所谓依赖属性.白话讲就是添加一个公开的属性. 同样,依赖属性的用法和附加属性的用法差不多. 依赖属性是具有一个get,set的属性,以及反调函数. 首先是声明依赖 ...

  6. C# 自定义用户控件报错 UserControl是“命名空间”,但此处被当做“类型”来使用

    今天从VS 2015转成VS 2022的时候遇到这样一个问题,经过多种测试后发现是因为程序目录下存放用户控件的文件夹名称是UserControl文件夹导致的,将文件夹名称改为UserControls后 ...

  7. 【转】页(page),用户控件(userControl),窗口(window)区别

    欢迎加入BIM行业开发交流1群 群号:711844216 背景 大家在vs中新建wpf项目后,会发现在添加新建项时会出现下列三个选项 它们有什么区别呢? 区别: 页:通常用于网页 窗口:通常一个桌面a ...

  8. C# 在用户控件中添加自定义事件

    /// <summary> /// 用户控件 /// </summary> public partial class UCMyControl : UserControl {// ...

  9. 在asp.net中为Web用户控件添加属性和事件

    在90年代初,Microsoft为Web程序员提供的 Active Server Pages(ASP)革命性地改变了Web的编程.它可以利用十分易用的模型在Web服务器上动态生成HTML,并且很容易的 ...

最新文章

  1. 人工智能产业发展联盟公布首轮AI芯片基准评测结果,评估工具已开源
  2. 【深度学习入门到精通系列】特别正经的合理调参介绍~❤️
  3. python - socket模块1
  4. CodeForces - 570E(dp------------- Codeforces Round #316 (Div. 2)E
  5. CPU占用率过高和OOM场景下如何排查问题
  6. 猫猫学iOS 之微博项目实战(2)微博主框架-自己定义导航控制器NavigationController
  7. 兵器类专业学c语言吗,第四轮学科评估C类学科如何?学姐:重点大学,热门专业放心读...
  8. 再谈如何写好技术文档?
  9. 《高质量程序设计指南——C++/C》(第三版)
  10. 省市区的行政区域数据2021(国家统计局)
  11. Typora实用快捷键
  12. 计算机组装专周实训心得,电脑组装实训心得3篇
  13. linux识别riser卡,一种具有多接口的Riser卡的制作方法
  14. github用户followers分析
  15. Android 7.0 APN 拨号上网流程分析
  16. jQuery雪花插件JQuery-Snowfall Plugin
  17. CubeMX+Eclipse+Jlink STM32开发环境搭建
  18. 儿童护眼灯怎么选?儿童护眼灯品牌排行榜
  19. java apache tomcat,Java 环境搭建+ Apache tomcat
  20. 现代密码学:数字签名

热门文章

  1. 彩虹工单自动处理网站插件
  2. 流量卡官网源码【全解无后门】 修复添加教程
  3. linux read line,LINUX readline 库的使用,,
  4. linux能挂载的文件类型,mount命令是如何自动识别挂载点的文件系统类型的
  5. 二、Linxu的目录结构
  6. python描述器 有限状态机_笨办法学 Python · 续 练习 30:有限状态机
  7. java重命名excel_Java重命名Excel工作表并设置工作表及标签颜色
  8. 轻量在线人工客服系统 支持多商家+自动适配移动端
  9. 简约风车壁纸自动采集小程序源码
  10. PHP 国家电话区号 PHP Countries and their call codes with two letter abbreviations