当前位置: 首页 > news >正文

WPF用户控件和依赖属性

依赖属性

定义:自己没有值,由别人给他赋值

应用场景

  • 要求属性支持绑定(只有依赖属性可以绑定)
  • 自定义控件/扩展控件属性
  • 验证/强制回调
  • 继承
  • 附加属性(一种特殊的依赖属性)

创建依赖属性

快捷键propdp+tab(两次)

可以看到下面代码,创建依赖属性分为三步:定义,注册和包装

//包装
public int MyProperty
{get { return (int)GetValue(MyPropertyProperty); }set { SetValue(MyPropertyProperty, value); }
}// Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
//定义和注册
public static readonly DependencyProperty MyPropertyProperty =DependencyProperty.Register("MyProperty", typeof(int), typeof(ownerclass), new PropertyMetadata(0));

上面代码中Register里面参数内容

属性名称就是页面中控件属性名称,要和属性名称相同

所有者类型是这个属性所在的类的类型

属性元数据是初始属性数据

使用依赖属性来绑定用户控件内部控件属性

当我们将label和textbox作为一个用户控件使用时,要想对label的content属性或者textbox的text进行赋值,无法直接进行。

 userControl.xaml文件

在这里面做绑定,绑定自己定义在cs文件里面的依赖属性,绑定方式也要注意不能使用普通的绑定方式,​依赖属性​ 是控件的“内置属性”,需通过 ElementName来进行绑定,绑定源是控件而非默认的 DataContext

<UserControl x:Class="WPF_try.UserControl1"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:local="clr-namespace:WPF_try"mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800"x:Name="root"><Grid><StackPanel Orientation="Vertical"><Label x:Name="label" Content="{Binding LabelText, ElementName=root}"/><TextBox x:Name="textBox" Width="200" Text="{Binding Text, ElementName=root, Mode=TwoWay}"/></StackPanel></Grid>
</UserControl>

userControl.cs文件

依赖属性,作为整个用户控件对外的设置接口

 public partial class UserControl1 : UserControl{public UserControl1(){InitializeComponent();}// 标签文本属性public static readonly DependencyProperty LabelTextProperty =DependencyProperty.Register("LabelText",typeof(string),typeof(UserControl1),new PropertyMetadata("Default Label"));// 文本框内容属性public static readonly DependencyProperty TextProperty =DependencyProperty.Register("Text",typeof(string),typeof(UserControl1),new FrameworkPropertyMetadata(default(string),FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));public string LabelText{get { return (string)GetValue(LabelTextProperty); }set { SetValue(LabelTextProperty, value); }}public string Text{get { return (string)GetValue(TextProperty); }set { SetValue(TextProperty, value); }}}

mainwindow.xaml文件

将依赖属性和cs文件中的属性进行绑定指定他的初始值,或者使用赋值方式给与值

<StackPanel><local:UserControl1 LabelText="用户名" Text="{Binding UserName}"/><local:UserControl1 LabelText="密码" Text="{Binding Password}"/>
</StackPanel>

mainwindow.cs文件

  public partial class MainWindow : Window{public MainWindow(){DataContext = this;InitializeComponent();}private string _userName="sdad";public string UserName{get { return _userName; }set { _userName = value;  }}private string _password="123456";public string Password{get { return _password; }set { _password = value;  }}}

http://www.dtcms.com/a/469587.html

相关文章:

  • 位运算 和 逻辑运算 以及 位运算指令
  • 工地招聘网站广告设计与制作视频
  • C++右值语义解析
  • java-高级技术(单元测试、反射)
  • 厦门做网站公司有哪些邯郸
  • Spring Boot 项目集成 Gradle:构建、测试、打包全流程教程
  • 电商主要是做什么工作东莞seo收费
  • SAP MM 通用物料移动过账冲销接口分享
  • 设计logo免费网站电商网站对比表格
  • SAP Vendor Invoice Management by OpenText (VIM)
  • 用 PyQt5 + PyPDF2 做一个“智能分页”的大 PDF 拆分器(含 GUI 与命令行双版本,附完整源码)
  • 芯片结构简介-arm/x86
  • Agentic AI 教程与 AI 编程入门:从基础到实战(含代码、流程图与 Prompt)
  • 做一个公司网站一般多少钱自己做的网站怎么放视频
  • 开源软件License科普:GPL/LGPL/Apache/木兰等license解析
  • React闭包陷阱(stale closure)介绍(React状态更新引用旧值)解决方法:使用函数式更新写法
  • 【Java数据结构】Map 与 Set 接口全解析
  • 海洋做网站大连网上办事大厅
  • 创新平台网站建设方案wordpress 恶意代码
  • Jupyter Notebook/Lab的高级技巧与快捷键
  • Request 和 Response 都使用了 Fetch API 的 Body 混入
  • 大数据毕业设计选题推荐-基于大数据的人体体能活动能量消耗数据分析与可视化系统-大数据-Spark-Hadoop-Bigdata
  • 电子电气架构 --- 操作系统的发展趋势
  • R语言绘图神器| ggplot2与其基本用法介绍
  • 自动负氧离子监测站:科技赋能,精准守护清新空气
  • 商务卫士包括网站建设seo优化谷歌
  • java-代码随想录第66天|Floyd 算法、A * 算法精讲 (A star算法)
  • 外贸展示网站多少钱企业画册内容
  • 上门做网站哪里有wordpress调用网页
  • 【部署python网站】宝塔面板 小目标2:实时搜索网上资源文件网站放在服务器上 用AI做一个作品,不断迭代。