如何使用 XAML/C# 在我的 UWP 应用程序中允许双向数据绑定?

How do I allow two way databinding in my UWP Application with XAML/C#?

这个数据绑定让我难住了。我正在尝试将按钮的文本绑定到我的玩家生活的文本。我关注了其他一些文章,并认为我把它放下了,应用程序运行但按钮文本仍然空白。这是我的 XAML:

<Page.DataContext>
    <local:Player x:Name="PlayerCur"></local:Player>
</Page.DataContext>
<Grid Background="#FFFFFFFF">
<StackPanel Margin="0,0,181,0">
<TextBlock x:Name="LifeTitle" TextAlignment="Center" FontSize="40" FontWeight="Bold" HorizontalAlignment="Center" VerticalAlignment="Center"  TextWrapping="Wrap" Text="Life" Height="56" Width="139"/>
        <Button Name="Life" Content="{Binding PlayerLife}" FontSize="50" FontWeight="Bold" HorizontalAlignment="Center" VerticalAlignment="Center" Height="126" Width="139">
            <Button.Flyout>
                <MenuFlyout>
                    <MenuFlyoutItem Text="Reset Life" Click="Reset_Life_Click"/>
                    <MenuFlyoutSeparator/>
                    <MenuFlyoutSubItem x:Name="lAdd" Text="Heal"   >
                        <MenuFlyoutItem x:Name="lAddone" Text="+1" Click="Add_OneL_Click"/>
                        <MenuFlyoutItem x:Name="lAddfive" Text="+5"  Click="Add_FiveL_Click"/>
                        <MenuFlyoutItem x:Name="lAddten" Text="+10" Click="Add_TenL_Click"/>
                    </MenuFlyoutSubItem>
                    <MenuFlyoutSubItem x:Name="lSubtract" Text="Deal Damage">
                        <MenuFlyoutItem x:Name="lSubone" Text="-1" Click="Sub_OneL_Click"/>
                        <MenuFlyoutItem x:Name="lSubthree" Text="-5" Click="Sub_FiveL_Click"/>
                        <MenuFlyoutItem x:Name="lSubfive" Text="-10" Click="Sub_TenL_Click"/>
                    </MenuFlyoutSubItem>


                    <MenuFlyoutSeparator></MenuFlyoutSeparator>
                </MenuFlyout>
            </Button.Flyout>
        </Button>
    </StackPanel>

这是播放器 Class:

    namespace App1.Classes
    {
        public class Player: INotifyPropertyChanged
       {
    private string _PlayerLife;
    private string _PlayerPoison;
    private int _life = 20;
    private int _poison = 0;
    public string PlayerLife
    {
        get { return _PlayerLife; }

    }
    public string PlayerPoison
    {
        get { return _PlayerPoison; }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
    public void reset(int destination)
    {
        if(destination==0)
        {
            _life = 20;
            OnPropertyChanged("Life");
        }
        else if(destination==1)
        {
            _poison = 0;
            OnPropertyChanged("Poison");
        }
        else
        {

        }
    }

    public void incrementer(int destination,int modifier)
    {
        if(destination==0)
        {
            _life = _life + modifier;
            _PlayerLife = _life.ToString();
            OnPropertyChanged("life");
        }
        else if(destination==1)
        {
            _poison = _poison + modifier;
            _PlayerPoison = _poison.ToString();
            OnPropertyChanged("poison");
        }
        else
        {

        }
    }
}

我觉得问题出在我的 class 没有通知 XAML 生命值和毒药变化时。

任何 suggestions/pointers 非常感谢。

真正的简单。 只需输入 Content="{Binding PlayerLife, **Mode=TwoWay**}" (那些星号只是用来表示新代码,不要实际放入)

OnPropertyChanged() 方法不正确,因为它们需要与属性名称完全匹配。这些属性是 PlayerLife 和 PlayerPoison,但您的 OnPropertyChanged 调用使​​用 "Life"、"Poison"、"life" 和 "poison"。尝试

OnPropertyChange(nameof(PlayerLife));