首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >WPF 中如何绑定附加属性?XAML 中记得加括号,C# 中记得不能用字符串

WPF 中如何绑定附加属性?XAML 中记得加括号,C# 中记得不能用字符串

作者头像
walterlv
发布2020-04-02 21:22:02
2.5K0
发布2020-04-02 21:22:02
举报

WPF 中如何绑定附加属性?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.RowWalterlv.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);

因此需要特别注意,附加属性的绑定不再能使用字符串,需要使用依赖项属性。


参考资料

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-04-01 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • WPF 中如何绑定附加属性?XAML 中记得加括号,C# 中记得不能用字符串
    • 背景代码
      • 在 XAML 中绑定附加属性
        • 在 C# 代码中绑定附加属性
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档