我有一个像这样的ViewModel:
dispatch()
我也有这样的观点:
class MyView(View):
def dispatch(self, request, *args, **kwargs):
import pdb; pdb.set_trace() # or print debug statements
super(MyView, self).dispatch(request, *args, **kwargs)
我遇到的问题是,当我点击“7”按钮时,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.");
}
}
}
值会在更新时更新(例如它将7添加到字符串中),但是<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>
即使我绑定它也不会显示更新的字符串。
我试过了:
CurrentNumber
设置为textblock
Mode
设置为TwoWay
我相信我已经按照正确的步骤实现了INotifyExecption,因为每次单击按钮时都会引发属性,并且调用setter调用命令的所有内容,我似乎无法在其他任何地方找到答案。
感谢您的帮助。
答案 0 :(得分:4)
在NotifyPropertyChanged()
方法中,您必须将属性名称作为参数传递才能使其正常工作,目前您正在传递导致问题的其他一些文本:
set
{
_currentNumber = value;
NotifyPropertyChanged("CurrentNumber");
}