0

我有一些代码,但在尝试更改“Player”类的值时遇到问题

我的 XAML

<Rectangle x:Name="oChar" Fill="Orange" Height="32" Width="32" Canvas.Left="32" Canvas.Top="{Binding Mode=TwoWay}"/>

后面的代码:

    private Player myPlayer = new Player { left = 32, top = 128 };
    public MainPage()
    {
        this.InitializeComponent();

        this.DataContext = myPlayer.top;
        myPlayer.top += 32;
    }

INotiftyProperty 类

    private int _top;

    public int top
    {

        get { return _top; }

        set { _top = value; OnPropertyChanged("top"); }

    }


    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

第一次做像 WPF 这样的东西,任何关于为什么值不会改变的帮助或任何信息都会很棒!

4

1 回答 1

1

你应该使用 this.Datacontext =myPlayer;

于 2013-10-10T04:56:10.777 回答