WPF似乎找不到自定义的附加属性

WPF似乎找不到自定义的附加属性,wpf,binding,dependency-properties,attached-properties,Wpf,Binding,Dependency Properties,Attached Properties,我正在尝试将属性(Button.Background)绑定到自定义附加属性上的属性 我有一份C#文件 public static class Square { public static readonly DependencyProperty PlayerProperty = DependencyProperty.RegisterAttached ( name : "Player", pr

我正在尝试将属性(Button.Background)绑定到自定义附加属性上的属性

我有一份C#文件

public static class Square
{
    public static readonly DependencyProperty PlayerProperty =
        DependencyProperty.RegisterAttached
        (
            name           : "Player",
            propertyType   : typeof(Player),
            ownerType      : typeof(UIElement),
            defaultMetadata: new FrameworkPropertyMetadata(null)
        );

    public static Player GetPlayer(UIElement element)
    {
        return (Player)element.GetValue(PlayerProperty);
    }

    public static void SetPlayer(UIElement element, Player player)
    {
        element.SetValue(PlayerProperty, player);
    }

    // Other attached properties
}
我的XAML的一个片段是

<Grid Name="board" Grid.Row="1" Grid.Column="1">
    <Grid.Resources>
        <Style TargetType="{x:Type Button}">
            <Setter Property="Height" Value="20" />
            <Setter Property="Width" Value="20" />
            <Setter Property="BorderThickness" Value="3" />
            <Setter Property="Background"
                Value="{Binding Path=(l:Square.Player).Brush, Mode=OneWay}" />
        </Style>
    </Grid.Resources>
</Grid>

这是我得到的错误:

无法将属性“Path”中的字符串“(l:Square.Player).Brush”转换为类型为“System.Windows.PropertyPath”的对象

属性路径无效。'Square“”没有名为“Player”的公共属性

标记文件“Gobang.Gui”中的对象“System.Windows.Data.Binding”出错;组件/主窗口。xaml'行148位置59


但是由于
Player
是位于
Square
上的附加属性,因此上述代码应该可以工作,对吗?

你不能用你正在做的方式设置绑定-你需要一个
Square
Player
的实例来绑定它。

你不能用你正在做的方式设置绑定-你需要一个
Square
Player
的实例来绑定它。

我相信您的附加属性应将Square指定为所有者,而不是UIElement

public static readonly DependencyProperty PlayerProperty =
    DependencyProperty.RegisterAttached("Player", typeof(Player),
        typeof(Square), new FrameworkPropertyMetadata(null));

我相信你的附属财产应该指定Square为所有者,而不是UIElement

public static readonly DependencyProperty PlayerProperty =
    DependencyProperty.RegisterAttached("Player", typeof(Player),
        typeof(Square), new FrameworkPropertyMetadata(null));
我让它工作了。 注意:它是只读属性,助手类必须从DependencyObject继承

public class Helper : DependencyObject
{
    public static readonly DependencyPropertyKey IsExpandedKey = DependencyProperty.RegisterAttachedReadOnly(
        "IsExpanded", typeof(bool), typeof(Helper), new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.Inherits));

    public static readonly DependencyProperty IsExpandedProperty = IsExpandedKey.DependencyProperty;

    public static bool GetIsExpanded(DependencyObject d)
    {
        return (bool)d.GetValue(IsExpandedKey.DependencyProperty);
    }

    internal static void SetIsExpanded(DependencyObject d, bool value)
    {
        d.SetValue(IsExpandedKey, value);
    }
}
我让它工作了。 注意:它是只读属性,助手类必须从DependencyObject继承

public class Helper : DependencyObject
{
    public static readonly DependencyPropertyKey IsExpandedKey = DependencyProperty.RegisterAttachedReadOnly(
        "IsExpanded", typeof(bool), typeof(Helper), new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.Inherits));

    public static readonly DependencyProperty IsExpandedProperty = IsExpandedKey.DependencyProperty;

    public static bool GetIsExpanded(DependencyObject d)
    {
        return (bool)d.GetValue(IsExpandedKey.DependencyProperty);
    }

    internal static void SetIsExpanded(DependencyObject d, bool value)
    {
        d.SetValue(IsExpandedKey, value);
    }
}

我没有尝试绑定到Square的实例,Square是我附加属性声明的位置。我没有尝试绑定到Square的实例,Square是我附加属性声明的位置。这就是我没有阅读intellisense工具提示的原因,叹气。绑定不太有效(背景没有改变),但这应该只是一些愚蠢的事情,我明天再看。非常感谢,我在这上面浪费了这么多时间。这就是我没有阅读intellisense工具提示的原因,叹气。绑定不太有效(背景没有改变),但这应该只是一些愚蠢的事情,我明天再看。非常感谢,我在这件事上浪费了很多时间。