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

标签 c# xaml uwp uwp-xaml

这个数据绑定(bind)让我很困惑。我正在尝试将按钮的文本绑定(bind)到我的玩家生活的文本。我关注了其他一些文章,并认为我把它放下了,应用程序运行但按钮文本仍然空白。这是我的 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。

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

最佳答案

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

关于c# - 如何使用 XAML/C# 在我的 UWP 应用程序中允许双向数据绑定(bind)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44488607/

相关文章:

c# - 如何绑定(bind)到 UWP 中的附加属性?

c# - 永远迷失在 if、while 和 foreach 语句中——无法获得适当的运行条件

c# - 如何在 XAML (Silverlight) 的资源字典中绑定(bind) `Path.Data`

c# - 将 SqlDataReader 传递给多个线程时是否应该通过引用传递 SqlDataReader

javascript - 我的 signalR 客户端缺少哪些基本组件?

c# - 如何阻止用户在不禁用列表框中选择项目

c# - ASP.Net Core 401 中的 SignalR 未经授权

c# - 如何在设置了ItemsSource的ComboBox顶部插入 “Select All”项目?

xaml - MVVM 将自定义属性绑定(bind)到 View 模型

.NET Native 和 RCW 开销