wpf 绑定到附加属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19769263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 10:00:32  来源:igfitidea点击:

Binding to attached property

c#wpfattached-properties

提问by Milosz Krajewski

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.

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

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 isexecuted, so attaching it to button works. GetAttachedText is never executed though, so it does not find property when resolving.

我已经在 SetAttachedText 和 GetAttachedText 上设置了断点,并且 SetAttachedText执行,因此将它附加到按钮工作。虽然 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,

我错过了什么?谢谢,

回答by Clemens

Your attached property registration is wrong. The ownerTypeis 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- 注册依赖属性的所有者类型