问候伙计们,希望有人有更新鲜的眼光,可以帮助我在这里查明问题,我正在尝试使用 prism 和 MVVM 模式创建一个小应用程序,到目前为止一切都很好,我的命令正在正确触发参数,但是,此处的 TextBlock 并未按应有的方式从其视图模型绑定到 CurrentUserKey 属性。
有人有什么想法吗?提前致谢...
LoginView.xaml(为简洁起见仅相关部分)...
<Grid DataContext="{Binding Path=., Source={StaticResource viewModel}}">
<Grid Margin="10">
<Label VerticalAlignment="Center" HorizontalAlignment="Right">Enter your Key:</Label>
<TextBlock Name="txtUserKey" Text="{Binding Path=CurrentUserKey}" Margin="2" />
<Button cal:Click.Command="{Binding GenericButtonClick}" cal:Click.CommandParameter="7">7</Button>
<Button cal:Click.Command="{Binding GenericButtonClick}" cal:Click.CommandParameter="8">8</Button>
<Button cal:Click.Command="{Binding GenericButtonClick}" cal:Click.CommandParameter="9">9</Button>
...
</Grid>
...
登录视图模型.cs
public class LoginViewModel : ViewModelBase
{
public LoginViewModel()
{
GenericButtonClick = new DelegateCommand<string>(GenericButtonClickHandler);
}
private void GenericButtonClickHandler(string argument)
{
if (argument.Length < 2) {
CurrentUserKey += argument;
}
RaisePropertyChangedEvent("GenericButtonClick");
}
public string CurrentUserKey { get; set; }
private ICommand GenericButtonClick { get; set; }
}
ViewModelBase.cs
public class ViewModelBase:INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChangedEvent(string Property_name)
{
if (Property_name == null) return;
PropertyChangedEventArgs e = new PropertyChangedEventArgs(Property_name);
PropertyChanged(this, e);
}
}