WPF:依赖属性的数据绑定
2015-05-06 11:10
357 查看
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”
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 依赖属性,用户控件依赖属性(DependencyProperty 依赖属性、GetValue() SetValue() CLR属性包装器、SetBinding 数据绑定)
- WPF依赖属性(续)(4)依赖属性与数据绑定
- 模拟实现WPF的依赖属性及绑定通知机制(2)--依赖对象的准备
- 模拟实现WPF的依赖属性及绑定通知机制(2)--依赖对象的准备
- 模拟实现WPF的依赖属性及绑定通知机制(3)--依赖对象
- 模拟实现WPF的依赖属性及绑定通知机制(3)--依赖对象
- 模拟实现WPF的依赖属性及绑定通知机制(4)--模拟实现绑定连动机制
- 模拟实现WPF的依赖属性及绑定通知机制(4)--模拟实现绑定连动机制
- 【转】WPF中PasswordBox控件的Password属性的数据绑定
- WPF依赖属性,绑定通知机制系列补充说明
- WPF依赖属性,绑定通知机制系列补充说明
- WPF的xaml中导入其他命名空间以及绑定类属性数据
- UWP深入学习三:依赖属性、附加属性和数据绑定
- WPF——数据绑定及属性改变事件
- 《深入浅出WPF》学习笔记二数据绑定(Binding)、依赖属性和附加属性
- WPF ----在UserControl的xaml里绑定依赖属性
- 模拟实现WPF的依赖属性及绑定通知机制(1)--依赖属性 .
- WPF 数据绑定 1_1 基础知识&绑定到元素属性