本文介绍了绑定到附加属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 Button 的 ContentTemplate 绑定到附加属性.我阅读了类似于绑定到附加属性"问题的所有答案,但我没能解决问题.

请注意,此处提供的示例是我的问题的简化版本,以避免将问题与业务代码混淆.

所以,我确实有带有附加属性的静态类:

使用 System.Windows;命名空间 AttachedPropertyTest{公共静态类扩展器{public static readonly DependencyProperty AttachedTextProperty =DependencyProperty.RegisterAttached("附加文本",类型(字符串),类型(依赖对象),新的 PropertyMetadata(string.Empty));公共静态无效SetAttachedText(DependencyObject obj,字符串值){obj.SetValue(AttachedTextProperty, value);}公共静态字符串 GetAttachedText(DependencyObject obj){返回(字符串)obj.GetValue(AttachedTextProperty);}}}

和一个窗口:

<网格><Button local:Extender.AttachedText="Attached"><文本块文字="{绑定RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Button},Path=(local:Extender.AttachedText)}"/></按钮></网格></窗口>

差不多就是这样.我希望在按钮中间看到附加".相反,它崩溃了:属性路径无效.Extender"没有名为AttachedText"的公共属性.

我在 SetAttachedText 和 GetAttachedText 上设置了断点,并且 SetAttachedText 执行,因此将其附加到按钮上是可行的.但是 GetAttachedText 永远不会被执行,所以它在解析时没有找到属性.

我的问题实际上更复杂(我正在尝试从 App.xaml 中的 Style 内部进行绑定),但让我们从简单的问题开始.

我错过了什么吗?谢谢,

解决方案

您的附加属性注册错误.ownerTypeExtender,而不是 DependencyObject.

public static readonly DependencyProperty AttachedTextProperty =DependencyProperty.RegisterAttached("附加文本",类型(字符串),typeof(Extender),//这里新的 PropertyMetadata(string.Empty));

有关注册附上的 MSDN 文档:>

ownerType - 注册依赖属性的所有者类型

I'm trying to bind from Button's ContentTemplate to attached property. I read all the answers for question similar to "binding to attached property" but I had no luck resolving the problem.

Please note that example presented here is a dumbed down version of my problem to avoid cluttering the problem with business code.

So, I do have static class with attached property:

using System.Windows;

namespace AttachedPropertyTest
{
  public static class Extender
  {
    public static readonly DependencyProperty AttachedTextProperty =
      DependencyProperty.RegisterAttached(
        "AttachedText",
        typeof(string),
        typeof(DependencyObject),
        new PropertyMetadata(string.Empty));

    public static void SetAttachedText(DependencyObject obj, string value)
    {
      obj.SetValue(AttachedTextProperty, value);
    }

    public static string GetAttachedText(DependencyObject obj)
    {
      return (string)obj.GetValue(AttachedTextProperty);
    }

  }
}

and a window:

<Window x:Class="AttachedPropertyTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:AttachedPropertyTest"
        Title="MainWindow" Height="350" Width="525">
  <Grid>
    <Button local:Extender.AttachedText="Attached">
      <TextBlock
        Text="{Binding
          RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Button},
          Path=(local:Extender.AttachedText)}"/>
    </Button>
  </Grid>
</Window>

That's pretty much it. I would expect t see "Attached" in the middle of the button.Instead it crashes with: Property path is not valid. 'Extender' does not have a public property named 'AttachedText'.

I've set breakpoints on SetAttachedText and GetAttachedText, and SetAttachedText is executed, so attaching it to button works. GetAttachedText is never executed though, so it does not find property when resolving.

My problem is actually more complicated (I'm trying to do binding from inside of Style in App.xaml) but let's start with the simple one.

Did I miss something?Thanks,

解决方案

Your attached property registration is wrong. The ownerType is Extender, not DependencyObject.

public static readonly DependencyProperty AttachedTextProperty =
    DependencyProperty.RegisterAttached(
        "AttachedText",
        typeof(string),
        typeof(Extender), // here
        new PropertyMetadata(string.Empty));

See the MSDN documentation for RegisterAttached:

这篇关于绑定到附加属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 22:23