C# 对多个实例使用依赖项属性

C# 对多个实例使用依赖项属性,c#,wpf,xaml,dependency-properties,C#,Wpf,Xaml,Dependency Properties,我基本上和这家伙有同样的问题: 我们之间的主要区别在于,我希望订阅一个事件,当xaml更改属性时,该事件可以访问本地实例 所以我有我的用户控制: public partial class BoardSquare : UserControl { public BoardSquare() { InitializeComponent(); Location = new BoardLocation(Int32.MinValue, Int32.MinValu

我基本上和这家伙有同样的问题:

我们之间的主要区别在于,我希望订阅一个事件,当xaml更改属性时,该事件可以访问本地实例

所以我有我的用户控制:

public partial class BoardSquare : UserControl
{
    public BoardSquare()
    {
        InitializeComponent();
        Location = new BoardLocation(Int32.MinValue, Int32.MinValue);

        XPositionProperty =
             DependencyProperty.Register("XPosition", typeof(int),
             typeof(BoardSquare), new PropertyMetadata(
                 new PropertyChangedCallback((value, args) => 
                 { 
                     Location.X = (int)args.NewValue;
                     resetBackgroundColorToPosition();
                 })));
        YPositionProperty=
            DependencyProperty.Register("YPosition", typeof(int),
            typeof(BoardSquare), new PropertyMetadata(
                new PropertyChangedCallback((value, args)=>
                {
                    Location.Y = (int)args.NewValue;
                    resetBackgroundColorToPosition();
                })));
    }

    private void resetBackgroundColorToPosition()
    {
        this.Background = (Brush)(new ColorEnumToBrushesConverter()).Convert(Location.GetSquareColor(), typeof(BlackWhiteColor), null, null);
    }

    public readonly DependencyProperty XPositionProperty;
    public readonly DependencyProperty YPositionProperty;

    public int XPosition
    {
        get
        {
            return (int)GetValue(XPositionProperty);
        }
        set
        {
            SetValue(XPositionProperty, value);
        }
    }

    public int YPosition 
    { 
        get
        {
            return (int)GetValue(YPositionProperty);
        }
        set
        {
            SetValue(YPositionProperty, value);
        }
    }

    public BoardLocation Location { get; set; }
}
这是我的XAML:

<local:BoardSquare Grid.Column="3" Grid.Row="0" XPosition="3" YPosition="0"/>
<local:BoardSquare Grid.Column="4" Grid.Row="0" XPosition="4" YPosition="0"/>

如果我正确理解了您的问题,那么您正在尝试用自己的WPF证书属性覆盖已经存在的WPF证书属性。我认为这在这里没有必要。如果XPosition已经是WPF证书属性,那么您所要做的就是使用getter/setter在usercontrol中创建一个属性,setter调用INotifypropertychanged。在这种情况下,您不需要dependency属性,除非您只需要相同的名称但行为不同,为什么要这样做

PropertyChangedCallback第一个参数是本地实例(顺便说一句,为了避免混淆,最好将其命名为obj而不是value)。您必须将此
DependencyObject
强制转换为
BoardSquare
,仅此而已

public static readonly DependencyProperty XPositionProperty =
    DependencyProperty.Register("XPosition", typeof(int), typeof(BoardSquare),
                                new PropertyMetadata(new PropertyChangedCallback((obj, args) => {
                                    BoardSquare bs = obj as BoardSquare;
                                    bs.Location.X = (int)args.NewValue;
                                    bs.resetBackgroundColorToPosition();
                                })));

我没有试图覆盖现有的属性。XPosition和YPosition可以称为其他的东西。如果没有dependency属性,我无法在XAML中为它们赋值。我宁愿远离二传手,因为这样看起来更干净,但我可能会被迫这样做。很好,我觉得自己很愚蠢,因为我没有意识到obj就是我的例子。非常感谢你。
public static readonly DependencyProperty XPositionProperty =
    DependencyProperty.Register("XPosition", typeof(int), typeof(BoardSquare),
                                new PropertyMetadata(new PropertyChangedCallback((obj, args) => {
                                    BoardSquare bs = obj as BoardSquare;
                                    bs.Location.X = (int)args.NewValue;
                                    bs.resetBackgroundColorToPosition();
                                })));