双向绑定

时间:2013-10-10 01:26:00

标签: c# wpf xaml inotifypropertychanged

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

我的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;
    }

Class 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这样的事情,任何关于为什么价值不会改变或任何信息都会很棒的帮助!

1 个答案:

答案 0 :(得分:1)

你应该使用this.Datacontext = myPlayer;