WPF 绑定到 TextBlock 不更新

WPF binding to TextBlock not updating

我有一个像这样的 ViewModel:

class CalculatorViewModel : INotifyPropertyChanged
{
    private string _currentNumber = "0";


    public string CurrentNumber
    {
        get
        {
            return _currentNumber;
        }
        set
        {
            _currentNumber = value;
            NotifyPropertyChanged("Updated current number.");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void NotifyPropertyChanged(string info)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(info));
    }

    public Command ButtonPressedCommand => new Command(ButtonPressed);

    private void ButtonPressed(object obj)
    {
        var button = obj as Button;
        if (button != null)
        {
            CurrentNumber += button.Content.ToString();
        }
        else
        {
            throw new NullReferenceException("The object passed into the command cannot be casted to a button.");
        }
    }
}

我也有这样的看法:

<Window x:Name="window_Calculator" x:Class="Calculator.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Calculator"
        mc:Ignorable="d"
        Title="Calculator" Height="350" Width="281.091" ResizeMode="NoResize" WindowStartupLocation="CenterScreen">
    <Window.DataContext>
        <local:CalculatorViewModel />
    </Window.DataContext>
    <Grid>
        <TextBlock x:Name="textBlock" Margin="10,10,10,252" TextWrapping="Wrap" Text="{Binding CurrentNumber}" FontSize="29.333" MaxHeight="57" MaxWidth="235"/>
        <Button x:Name="button_7" Content="7" Margin="10,67,217,215" FontSize="18.667" MaxHeight="37" MaxWidth="46"  Command="{Binding ButtonPressedCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Self}}"/>
        <Button x:Name="button_8" Content="8" Margin="61,67,166,215" FontSize="18.667" MaxHeight="37" MaxWidth="46"/>
        <Button x:Name="button_9" Content="9" Margin="112,67,115,215" FontSize="18.667" MaxHeight="37" MaxWidth="46"/>
        <Button x:Name="button_4" Content="4" Margin="10,109,217,173" FontSize="18.667" MaxHeight="37" MaxWidth="46"/>
        <Button x:Name="button_5" Content="5" Margin="61,109,166,173" FontSize="18.667" MaxHeight="37" MaxWidth="46"/>
        <Button x:Name="button_6" Content="6" Margin="112,109,115,173" FontSize="18.667" MaxHeight="37" MaxWidth="46"/>
        <Button x:Name="button_1" Content="1" Margin="10,151,217,131" FontSize="18.667" MaxHeight="37" MaxWidth="46"/>
        <Button x:Name="button_2" Content="2" Margin="61,151,166,131" FontSize="18.667" MaxHeight="37" MaxWidth="46"/>
        <Button x:Name="button_3" Content="3" Margin="112,151,115,131" FontSize="18.667" MaxHeight="37" MaxWidth="46"/>
        <Button x:Name="button_plus" Content="+" Margin="217,67,10,215" FontSize="18.667" MaxHeight="37" MaxWidth="46"/>
        <Button x:Name="button_minus" Content="-" Margin="217,109,10,173" FontSize="18.667" MaxHeight="37" MaxWidth="46"/>
        <Button x:Name="button_multiply" Content="*" Margin="217,151,10,131" FontSize="18.667" MaxHeight="37" MaxWidth="46"/>
        <Button x:Name="button_divide" Content="\" Margin="217,193,10,89" FontSize="18.667" MaxHeight="37" MaxWidth="46"/>
        <Button x:Name="button_equals" Content="=" Margin="217,262,10,10" FontSize="18.667" MaxHeight="37" MaxWidth="46"/>
    </Grid>
</Window>

我遇到的问题是,当我单击“7”按钮时,CurrentNumber 值在 degugging 更新时应该像它应该的那样(即它附加 7 到字符串),但是 textblock 不显示更新后的字符串,即使我已经绑定了它。

我试过:

我相信我已经按照正确的步骤来实现 INotifyExecption,因为每次单击按钮时都会引发 属性,并且调用命令时会调用 setter,我可以'似乎在其他地方也找不到答案。

感谢您的帮助。

NotifyPropertyChanged() 方法中,您必须传递 属性 名称作为参数才能使其工作,目前您传递的是其他导致问题的文本:

    set
    {
        _currentNumber = value;
        NotifyPropertyChanged("CurrentNumber");
    }

供参考see this tutorial