1

我基本上和这里的这个人有同样的问题:当表单上放置多个控件时,会引发错误:“'Subjects' 属性已由 'Period' 注册”

我们之间的主要区别在于,当 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"/>

据我了解,解决方案是将 XPositionProperty 设为静态,然后将其注册到静态构造函数中。然后我的问题是当我的 PropertyChangeCallback 事件发生时我无法访问该类的本地实例。

如何在 XAML 中设置属性并仍然在 C# 代码中获得属性更改事件?

有比依赖属性更好的解决方案吗?


以下是我实现答案后 BoardSquare 的工作代码。

    public partial class BoardSquare : UserControl
{
    static BoardSquare()
    {
        XPositionProperty =
             DependencyProperty.Register("XPosition", typeof(int),
             typeof(BoardSquare), new PropertyMetadata(
                 new PropertyChangedCallback((objectInstance, args) =>
                 {
                     BoardSquare boardSquare = (BoardSquare)objectInstance;
                     boardSquare.Location.X = (int)args.NewValue;
                     boardSquare.resetBackgroundColorToPosition();
                 })));
        YPositionProperty =
            DependencyProperty.Register("YPosition", typeof(int),
            typeof(BoardSquare), new PropertyMetadata(
                new PropertyChangedCallback((objectInstance, args) =>
                {
                    BoardSquare boardSquare = (BoardSquare)objectInstance;
                    boardSquare.Location.Y = (int)args.NewValue;
                    boardSquare.resetBackgroundColorToPosition();
                })));
    }

    public BoardSquare()
    {
        InitializeComponent();
        Location = new BoardLocation(Int32.MinValue, Int32.MinValue);


    }

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

    public static readonly DependencyProperty XPositionProperty;
    public static 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; }
}
4

2 回答 2

2

The first argument of PropertyChangedCallback is the local instance (btw. you better name it obj than value to avoid confusion). You have to cast this DependencyObject to BoardSquare and that's all.

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();
                                })));
于 2013-09-04T00:23:16.037 回答
0

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

于 2013-09-03T22:57:48.457 回答