【发布时间】:2024-05-01 04:05:02
【问题描述】:
我正在尝试将一个属性 (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 的 sn-p 是
<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;component/mainwindow.xaml”第 148 行位置 59 中的对象“System.Windows.Data.Binding”出错。
但是由于Player 是Square 上的附加属性,所以上面的代码应该可以工作,对吧?
【问题讨论】:
标签: wpf binding dependency-properties attached-properties