WPF:依赖属性的数据绑定
One of the strengths of WPF is its data binding capabilities. Although data binding is not new (in fact winforms has some limited data binding support) WPF takes it to the next level. In this post I’ll show you how to bind an element to a property defined in the code behind.
How it’s done?
In order to bind a property to an element on the control we need to do the following:
- Create a dependency property
- Name your user control – in XAML
- Bind the property to an element of your choice
Step 1 – Create a dependency property
Dependency properties are simple .NET properties done the WPF way. On the surface they look just like the properties we use since .NET 1.x came out, underneath they are actually stored in a dictionary like collection and that implementation is what makes WPF work the way it does.
You don’t need to actually know how dependency properties work in order to create them but if you do want to learn more there is a good overview on MSDN.
Adding dependency property is done by creating a new field and initializing it usingDependencyProperty.Register and then using that field in the property’s setter and getter:
public partial class Window1 : Window
{
public static DependencyProperty SomeTextProperty =
DependencyProperty.Register("SomeText", typeof(string), typeof(Window1));
public string SomeText
{
get { return (string)GetValue(SomeTextProperty); }
set { SetValue(SomeTextProperty, value); }
}
Make sure that the name of the property used to register the property is exactly the same as the property name- otherwise it won’t work.
We’re done with the “code” part – now let’s head to the XAML and create the actual data binding.
Step 2 – Define the control’s name
In order to use the property we need to address it and for that we need to define a name for our control. Choose a name and add Name=<user control name> at the beginning of the XAML file. It doesn't matter how you call the control as long as you use the same name in the actual binding.
Step 3 – Bind the property to an element
In this trivial example I’ve used the property to set (and get) the text that appears on a textbox – and the XAML should look something like this:
<Window x:Class="BindToProperty.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300" Name="UserControlName">
<Grid>
<TextBox Name="textBox1" Text="{Binding ElementName=UserControlName,Path=SomeText}"/>
</Grid>
</Window>
And now whenever you change the value of Window1.SomeText that text will be displayed on the textbox “automatically”
WPF:依赖属性的数据绑定的更多相关文章
- WPF依赖属性详解
WPF依赖属性详解 WPF 依赖属性 英文译为 Dependency Properties,是WPF引入的一种新类型的属性,在WPF中有着极为广泛的应用,在WPF中对于WPF Dependency P ...
- WPF依赖属性
原文:http://www.cnblogs.com/xiongpq/archive/2010/06/29/1767905.html 概述: Windows Presentation Foundatio ...
- WPF自学入门(五)WPF依赖属性
在.NET中有事件也有属性,WPF中加入了路由事件,也加入了依赖属性.最近在写项目时还不知道WPF依赖属性是干什么用的,在使用依赖项属性的时候我都以为是在用.NET中的属性,但是确实上不是的,通过阅读 ...
- WPF依赖属性(续)(2)依赖属性与附加属性的区别
原文:WPF依赖属性(续)(2)依赖属性与附加属性的区别 接上篇,感谢各位的评论,都是认为依赖属性的设计并不是为了节省内存,从大的方面而讲是如此.样式,数据绑定,动画样样都离不开它.这篇 ...
- WPF依赖属性值源(BaseValueSource)
原文:WPF依赖属性值源(BaseValueSource) WPF依赖属性提供一个机制,可以获取依赖属性提供值的来源 其以BaseValueSource枚举表示 1.Default public ...
- WPF依赖属性(续)(3)依赖属性存储
原文:WPF依赖属性(续)(3)依赖属性存储 在之前的两篇,很多朋友参与了讨论,也说明各位对WPF/SL计数的热情,对DP系统各抒已见,当然也出现了一些分歧. 以下简称DP为依赖属性 ...
- WPF依赖属性(续)(1)
原文:WPF依赖属性(续)(1) 之前有写过几篇文章,详细地介绍了依赖属性的基本使用方法,如果你不想了解其内部实现机制的话,那么通过那两篇文章的介绍,足以应付平时的应用 ...
- 监听WPF依赖属性
原文:监听WPF依赖属性 当我们使用依赖属性的时候,有时需要监听它的变化,这在写自定义控件的时候十分有用, 下面介绍一种简单的方法. 如下使用DependencyPropertyDescripto ...
- WPF依赖属性的正确学习方法
前言 我在学习WPF的早期,对依赖属性理解一直都非常的不到位,其恶果就是,我每次在写依赖属性的时候,需要翻过去的代码来复制黏贴. 相信很多朋友有着和我相同的经历,所以这篇文章希望能帮助到那些刚刚开始学 ...
随机推荐
- android app 内部文件路径
public class MainActivity extends Activity { final String FILE_NAME = "crazyit.bin"; @Over ...
- JAVA数据库基本操作 (转)
JAVA数据库基本操作指南 Java数据库操作基本流程:取得数据库连接 - 执行sql语句 - 处理执行结果 - 释放数据库连接. 一.取得数据库连接 1.用DriverManager取数据库连接 ...
- ArchLinux 安装笔记 --zz
为何安装 ArchLinux 为了更深层次的理解 Linux (其实只是闲的蛋疼 准备安装介质 U盘首选,没有之一.自己的本子是 MBR 的,UEFI 神马的我才不知道呢哼! 制作 U 盘启动: Li ...
- 谈谈Delphi中的类和对象4---类是一种对数据和操作高度的封装机制 && 类是一种代码重用机制
五.类是一种对数据和操作高度的封装机制 1)数据封装 unit Unit2; interface type TEmployee = class; private FName: String; publ ...
- 攻城狮在路上(壹) Hibernate(九)--- Hibernate的映射类型
Hibernate采用映射类型作为Java类型和SQL类型的桥梁,对应type属性.分为两种:内置映射类型和客户化映射类型.一.内置映射类型: 1.Java基本类型的Hibernate映射类型: Ja ...
- WPF MVVM模式下实现ListView下拉显示更多内容
在手机App中,如果有一个展示信息的列表,通常会展示很少一部分,当用户滑动到列表底部时,再加载更多内容.这样有两个好处,提高程序性能,减少网络流量.这篇博客中,将介绍如何在WPF ListView中实 ...
- 在WPF中使用CefSharp嵌入浏览器
日常开发中,我们需要将一些Web页面嵌入到桌面客户端软件中.下面我们使用CefSharp嵌入浏览器来实现. 首先先介绍一下CefSharp嵌入式浏览器,它是基于Google浏览器的一个组件,我们可以在 ...
- 第十九篇:提高SOUI应用程序渲染性能的三种武器
SOUI是一套100%开源的基于DirectUI的客户端开发框架. 基于DirectUI设计的UI虽然UI呈现的效果可以很炫,但是相对于传统的win32应用程序中每个控件一个窗口句柄的形式,渲染效率是 ...
- url地址中 "&" "/"等符号的转义处理(转)
URL出现了有+,空格,/,?,%,#,&,=等特殊符号的时候,可能在服务器端无法获得正确的参数值,如何是好? 解决办法:将这些字符转化成服务器可以识别的字符,对应关系如下: URL中的特殊字 ...
- 允许webservice远程测试
System.Web节点下添加 <webServices> <protocols> <add name= "HttpPost"/> <ad ...