2016-01-14 46 views
0

我有一個視圖模型,像這樣:WPF結合文本塊沒有更新

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的值在去除更新時應該如此(即它將7附加到字符串中),但textblock不會顯示更新後的字符串,即使已將其綁定。

我已經試過:

  • 設置ModeTwoWay
  • 設置UpdateSourceTriggerPropertyChanged
  • 一個以上
  • 以上都不是

相信的我已按照正確的步驟執行INotifyExeption每次點擊按鈕時都會產生屬性,並且調用者被稱爲調用該命令的所有內容,但我似乎無法在其他任何地方找到答案。

感謝您的幫助。

回答

3

NotifyPropertyChanged()方法你必須通過屬性名稱作爲參數傳遞給它的工作,目前要傳遞這是造成問題的一些其他文本:

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

僅供參考see this tutorial

+0

這是正確的回答。非常感謝您的參考。 –