有关 WPF 数据绑定到 ViewModel 属性 (Prism) 的帮助

发布于 2024-09-10 14:20:24 字数 1961 浏览 0 评论 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>
...

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回梦 2024-09-17 14:20:24

当 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文