有关 WPF 数据绑定到 ViewModel 属性 (Prism) 的帮助
问候大家, 希望有人有更新鲜的眼光,可以帮助我查明这里的问题,我正在尝试使用 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>
...
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);
}
}
Greetings guys,
hopefully somebody has fresher eyes and can help me pinpoint the problem here, I'm trying to create a small app with prism and the MVVM pattern, everything was working nicely up to this point, my commands are firing properly with the argument, however, the TextBlock here is not binding to the CurrentUserKey property from it's viewmodel as it should.
Anybody has any ideas?
thanks in advance...
LoginView.xaml (only relevant parts for brevity)
...
<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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当 CurrentUserKey 更改时,您不会引发 PropertyChanged。
此外,绑定到文本框中的文本还存在一些问题:请参阅 http://social.msdn.microsoft.com/forums/en-US/wpf/thread/c404360c-8e31-4a85-9762-0324ed8812ef/ 和 WPF:文本框文本未更新
You are not raising PropertyChanged when CurrentUserKey has changed.
Additionally, there are some issues with binding to Text in a TextBox: See http://social.msdn.microsoft.com/forums/en-US/wpf/thread/c404360c-8e31-4a85-9762-0324ed8812ef/ and WPF: TextBox Text is not updating