0

问候伙计们,希望有人有更新鲜的眼光,可以帮助我在这里查明问题,我正在尝试使用 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);
    }
}
4

1 回答 1

2

CurrentUserKey 已更改时,您不会提高 PropertyChanged。

此外,绑定到 TextBox 中的文本存在一些问题:请参阅http://social.msdn.microsoft.com/forums/en-US/wpf/thread/c404360c-8e31-4a85-9762-0324ed8812ef/WPF: TextBox 文本未更新

于 2010-07-16T06:45:02.827 回答