【发布时间】:2016-01-14 15:05:59
【问题描述】:
我有一个像这样的 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 值在调试更新时应该像它应该的那样(即它将 7 附加到字符串),但是 textblock 不显示即使我已经绑定了它,也更新了字符串。
我试过了:
- 将
Mode设置为TwoWay - 将
UpdateSourceTrigger设置为PropertyChanged - 以上之一
- 以上都不是
我相信我已经遵循了正确的步骤来实现 INotifyExecption,因为每次单击按钮时都会引发属性,并且每次调用命令时都会调用 setter,我似乎也无法在其他任何地方找到答案。
感谢您的帮助。
【问题讨论】:
标签: c# wpf xaml mvvm inotifypropertychanged