问候人, 希望有人有更新鲜的眼睛,可以帮助我在这里查明问题,我正在尝试创建一个带有棱镜和MVVM模式的小应用程序,到目前为止,一切都运行良好,我的命令正在使用参数正确触发,但是,这里的TextBlock没有像它应该的那样从它的viewmodel绑定到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>
...
LoginViewModel.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);
}
}
答案 0 :(得分:2)
当CurrentUserKey发生变化时,你不会提升PropertyChanged。
此外,在TextBox中绑定文本时存在一些问题:请参阅http://social.msdn.microsoft.com/forums/en-US/wpf/thread/c404360c-8e31-4a85-9762-0324ed8812ef/和WPF: TextBox Text is not updating