简体   繁体   English

绑定附属物

[英]Binding to attached property

I'm trying to bind from Button's ContentTemplate to attached property. 我正在尝试将Button的ContentTemplate绑定到附加属性。 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'. 'Extender'没有名为'AttachedText'的公共属性。

I've set breakpoints on SetAttachedText and GetAttachedText, and SetAttachedText is executed, so attaching it to button works. 我对SetAttachedText和GetAttachedText设置断点,SetAttachedText 执行,因此将其连接到按钮的作品。 GetAttachedText is never executed though, so it does not find property when resolving. GetAttachedText永远不会被执行,所以它在解析时找不到属性。

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. 我的问题实际上更复杂(我试图从App.xaml中的Style内部进行绑定)但是让我们从简单的开始。

Did I miss something? 我错过了什么? Thanks, 谢谢,

Your attached property registration is wrong. 您附属的财产登记是错误的。 The ownerType is Extender , not DependencyObject . ownerTypeExtender ,而不是DependencyObject

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

See the MSDN documentation for RegisterAttached : 有关RegisterAttached的信息,请参阅MSDN文档:

ownerType - The owner type that is registering the dependency property ownerType - 正在注册依赖项属性的所有者类型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM