简体   繁体   English

WPF绑定到TextBlock不会更新

[英]WPF binding to TextBlock not updating

I have a ViewModel like so: 我有一个像这样的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.");
        }
    }
}

I also have a view like so: 我也有这样的看法:

<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>

The problem I've having is that when I click the '7' button, the CurrentNumber value when degugging updates like it should (ie it appends 7 to the string), however the textblock does not display the updated string even though I've bound it. 我遇到的问题是,当我单击“ 7”按钮时,在对更新进行类似的调试时应使用CurrentNumber值(即,将7附加到字符串中),但是即使我已经执行了以下操作, textblock也不会显示更新的字符串绑定它。

I've tried: 我试过了:

  • Setting the Mode to TwoWay Mode设置为TwoWay
  • Setting the UpdateSourceTrigger to PropertyChanged UpdateSourceTrigger设置为PropertyChanged
  • One of the above 以上之一
  • None of the above 以上都不是

I believe I've followed the correct steps to implement INotifyExecption as property is being raised everytime the button is clicked and the setter is being called everything the command is called, I can't seem to find an answer anywhere else either. 我相信我已经执行了正确的步骤来实现INotifyExecption,因为每次单击按钮并调用setter时都会引发属性,而调用此命令的所有内容都被调用,我似乎也找不到其他任何答案。

Thanks for your help. 谢谢你的帮助。

In the NotifyPropertyChanged() method you have to pass property name as parameter to have it work, currently you are passing some other text which is causing problem : NotifyPropertyChanged()方法中,您必须传递属性名称作为参数才能使其正常工作,当前,您正在传递其他一些引起问题的文本:

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

For reference see this tutorial 供参考, 请参阅本教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM