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:

  1. Create a dependency property
  2. Name your user control – in XAML
  3. 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:依赖属性的数据绑定的更多相关文章

  1. WPF依赖属性详解

    WPF依赖属性详解 WPF 依赖属性 英文译为 Dependency Properties,是WPF引入的一种新类型的属性,在WPF中有着极为广泛的应用,在WPF中对于WPF Dependency P ...

  2. WPF依赖属性

    原文:http://www.cnblogs.com/xiongpq/archive/2010/06/29/1767905.html 概述: Windows Presentation Foundatio ...

  3. WPF自学入门(五)WPF依赖属性

    在.NET中有事件也有属性,WPF中加入了路由事件,也加入了依赖属性.最近在写项目时还不知道WPF依赖属性是干什么用的,在使用依赖项属性的时候我都以为是在用.NET中的属性,但是确实上不是的,通过阅读 ...

  4. WPF依赖属性(续)(2)依赖属性与附加属性的区别

    原文:WPF依赖属性(续)(2)依赖属性与附加属性的区别        接上篇,感谢各位的评论,都是认为依赖属性的设计并不是为了节省内存,从大的方面而讲是如此.样式,数据绑定,动画样样都离不开它.这篇 ...

  5. WPF依赖属性值源(BaseValueSource)

    原文:WPF依赖属性值源(BaseValueSource)   WPF依赖属性提供一个机制,可以获取依赖属性提供值的来源 其以BaseValueSource枚举表示 1.Default public ...

  6. WPF依赖属性(续)(3)依赖属性存储

    原文:WPF依赖属性(续)(3)依赖属性存储          在之前的两篇,很多朋友参与了讨论,也说明各位对WPF/SL计数的热情,对DP系统各抒已见,当然也出现了一些分歧. 以下简称DP为依赖属性 ...

  7. WPF依赖属性(续)(1)

    原文:WPF依赖属性(续)(1)                 之前有写过几篇文章,详细地介绍了依赖属性的基本使用方法,如果你不想了解其内部实现机制的话,那么通过那两篇文章的介绍,足以应付平时的应用 ...

  8. 监听WPF依赖属性

    原文:监听WPF依赖属性 当我们使用依赖属性的时候,有时需要监听它的变化,这在写自定义控件的时候十分有用, 下面介绍一种简单的方法.   如下使用DependencyPropertyDescripto ...

  9. WPF依赖属性的正确学习方法

    前言 我在学习WPF的早期,对依赖属性理解一直都非常的不到位,其恶果就是,我每次在写依赖属性的时候,需要翻过去的代码来复制黏贴. 相信很多朋友有着和我相同的经历,所以这篇文章希望能帮助到那些刚刚开始学 ...

随机推荐

  1. 模拟赛1103d1

    取模(mod) [题目描述] 有一个整数a和n个整数b_1, -, b_n.在这些数中选出若干个数并重新排列,得到c_1,-, c_r.我们想保证a mod c_1 mod c_2 mod - mod ...

  2. Oracle读写分离架构

    读写分离是架构分布式系统的一个重要思想.不少系统整体处理能力并不能同业务的增长保持同步,因此势必会带来瓶颈,单纯的升级硬件并不能一劳永逸.针对业务类型特点,需要从架构模式上进行一系列的调整,比如业务模 ...

  3. Swift - UIViewController

    UIViewController类详解: 通过Nib文件初始化 init(nibName nibName: String?, bundle nibBundle: NSBundle?) println( ...

  4. hdu 1005

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1005 思路:找规律题 #include<stdio.h> main() { ]; int ...

  5. vim 打开高亮和关闭高亮

    :set hls 找开高亮 :set nohls 关闭高亮

  6. JVM内存区域与内存溢出异常

    Java虚拟机在执行java程序时会把它所管理的内存会分为若干个不同的数据区域,不同的区域在内存不足时会抛出不同的异常. >>运行时数据区域的划分 (1)程序计数器程序计数器(Progra ...

  7. ***PHP中error_reporting()用法详解(含codeigniter框架中屏蔽错误提示的解决方案)

    php中我们对错误的处理会常用到error_reporting函数了,大家可以看到最多的是error_reporting(E_ALL ^ E_NOTICE)了,这个到底什么意思呢,下面我来来看看. e ...

  8. 设计模式学习之迭代器模式(Iterator,行为型模式)(17)

    参考地址:http://www.cnblogs.com/zhili/p/IteratorPattern.html 一.介绍迭代器是针对集合对象而生的,对于集合对象而言,必然涉及到集合元素的添加删除操作 ...

  9. 【转载】 Python动态生成变量

    用Python循环创建多个变量, 如创建 a1=   .a2=   .a3=   .a4=   .a5=    或  self.a1=    .self.a2=   . self.a3= 一. 可以通 ...

  10. ASP.NET 自定义URL重写 分类: ASP.NET 2014-10-31 16:05 175人阅读 评论(0) 收藏

    一.功能说明: 可以解决类似 http://****/news 情形,Url路径支持正则匹配. 二.操作步骤: 1.增加URL重写模块: using System; using System.IO; ...