内容简介:在 XAML 中写绑定是 WPF 学习的必修课,进阶一点的,是用 C# 代码来写绑定。然而一旦绑定的属性是附加属性,好多小伙伴就会开始遇到坑了。本文将介绍如何在 XAML 和 C# 代码中绑定附加属性。开始遇到这个问题的背景是我定义了一个附加属性,然后试图通过绑定的方式完成一些业务。
在 XAML 中写绑定是 WPF 学习的必修课,进阶一点的,是用 C# 代码来写绑定。然而一旦绑定的属性是附加属性,好多小伙伴就会开始遇到坑了。
本文将介绍如何在 XAML 和 C# 代码中绑定附加属性。
背景代码
开始遇到这个问题的背景是我定义了一个附加属性,然后试图通过绑定的方式完成一些业务。
用附加属性来完成的很大一个好处在于不需要改动原有的代码破坏原来的类。例如我只需要在任何一个类中定义 IsDraggable 附加属性,就可以让我其他地方的 Grid Button 等支持拖拽。
public class DraggableElement : FrameworkElement
{
static TabViewItem()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(DraggableElement),
new FrameworkPropertyMetadata(typeof(DraggableElement)));
}
public static readonly DependencyProperty IsDraggableProperty =
DependencyProperty.RegisterAttached(
"IsDraggable", typeof(bool), typeof(TabViewItem),
new PropertyMetadata(true));
public static bool GetIsDraggable(DependencyObject item)
=> (bool) item.GetValue(IsDraggableProperty);
public static void SetIsDraggable(DependencyObject obj, bool value)
=> obj.SetValue(IsDraggableProperty, value);
}
在 XAML 中绑定附加属性
在 XAML 中绑定附加属性的时候需要加上括号和类型的命名空间前缀:
<ListViewItem Content="{Binding (local:DraggableElement.IsDraggable), RelativeSource={RelativeSource Self}}"
local:DraggableElement.IsDraggable="True" />
对于 WPF 内置的命名空间( http://schemas.microsoft.com/winfx/2006/xaml/presentation 命名空间下),是不需要加前缀的。
<TextBlock x:Name="DemoTextBlock" Grid.Row="1"
Text="{Binding (Grid.Row), RelativeSource={RelativeSource Self}}" />
跟其他的绑定一样,这里并不需要在 Binding 后面写 Path= ,因为 Binding 的构造函数中传入的参数就是赋值给 Path 的。
在 C# 代码中绑定附加属性
上面在说明附加属性绑定的时候我特地额外写了一个不需要写命名空间的 XAML 绑定附加属性的代码,这是为了说明接下来写 C# 代码时的注意事项。
是这样写吗?
// 给不看全文的小伙伴:这段代码是无法工作的!正常工作的在后文。
Binding binding = new Binding("(Grid.Row)")
{
Source = DemoTextBlock,
}
BindingOperations.SetBinding(DemoTextBlock, TextBox.TextProperty, binding);
设想应该不是,因为 C# 代码中是没有命名空间前缀的,于是对于前面 XAML 中 (local:DraggableElement.IsDraggable) 的 local 部分就很不好处理。
实际上,这里的字符串即便是写成 System.Windows.Grid.Row 和 Walterlv.BindingDemo.DraggableElement.IsDraggable 也依然会绑定失败。
在 C# 代码中绑定附加属性,需要 使用依赖项属性,而不能使用字符串 !
Binding binding = new Binding
{
Source = DemoTextBlock,
Path = new PropertyPath(Grid.RowProperty),
}
BindingOperations.SetBinding(DemoTextBlock, TextBox.TextProperty, binding);
Binding binding = new Binding
{
Source = DemoDraggableElement,
Path = new PropertyPath(DraggableElement.IsDraggableProperty),
}
BindingOperations.SetBinding(DemoDraggableElement, TextBox.TextProperty, binding);
因此需要特别注意,附加属性的绑定不再能使用字符串,需要使用依赖项属性。
参考资料
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- 还记得第一个看到的Flutter组件吗?
- Go,Go,Go 明天上班记得定闹钟
- 还记得那些年你修改过的 DOM 吗
- 带你来搭建虚拟机和 Redis 集群,记得收藏
- Android 满 10 岁啦,还记得你是在哪年使用它的吗?
- 如果你习惯在Vue里使用css简写属性,记得避开这个坑
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Automate This
Christopher Steiner / Portfolio / 2013-8-9 / USD 25.95
"The rousing story of the last gasp of human agency and how today's best and brightest minds are endeavoring to put an end to it." It used to be that to diagnose an illness, interpret legal docume......一起来看看 《Automate This》 这本书的介绍吧!