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

时间:2017-06-11 20:44:51

标签: c# xaml uwp uwp-xaml

这个数据绑定令我难过。我试图将按钮的文本绑定到我的玩家生活的文本。我跟着其他一些文章,并认为我把它弄下来,应用程序运行,但按钮文本仍然是空白。这是我的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>

这是玩家类:

    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
        {

        }
    }
}

我觉得这个问题是我的班级在生命和毒药改变时没有通知XAML。

任何建议/指示都非常感谢。

2 个答案:

答案 0 :(得分:3)

真正的简单。 只需输入Content="{Binding PlayerLife, **Mode=TwoWay**}" (这些星号只是用于表示新代码,实际上并未将它们放入其中)

答案 1 :(得分:1)

OnPropertyChanged()方法不正确,因为它们需要完全匹配属性的名称。属性是PlayerLife和PlayerPoison,但你的OnPropertyChanged调用使用&#34; Life&#34;,&#34; Poison&#34;,&#34; life&#34;和&#34;毒药&#34;。试试

OnPropertyChange(nameof(PlayerLife));