如何设置一个视图模型绑定与ReactiveUI一个TextBox? [英] How to setup a ViewModel binding for a TextBox with ReactiveUI?

查看:526
本文介绍了如何设置一个视图模型绑定与ReactiveUI一个TextBox?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关某种原因,我不能让我的文本/属性绑定到工作自动与无UI。

For some reason I can't get my textbox/property binding to work "automatically" with Reactive UI.

我的DataContext的设置是这样的:

My DataContext is setup like this:

我的视图模型是为后面的MainWindow.xaml.cs的code设置:

My ViewModel is setup in the code behind for the MainWindow.xaml.cs:

public MainWindowViewModel ViewModel { get; protected set; }

public MainWindow()
{
    ViewModel = new MainWindowViewModel();
}

在我的WPF MainWindow.xaml我在DataContext和有这样一个文本框元素:

In my WPF MainWindow.xaml I have the DataContext and the have a textbox element like this:

(其它性能ommitted)

(Other properties ommitted)

<Window x:Name="Window">
    <Grid DataContext="{Binding ViewModel, ElementName=Window}">
        <StackPanel x:Name="ContentGrid" Grid.Row="1">
            <TextBox Name="Password" Text="{Binding Password, Mode=TwoWay}" />
            ...

在MainWindowViewModel我有一个属性,现场是这样的:

In the MainWindowViewModel I have a property and field like this:

string _PasswordConfirmation;

public string PasswordConfirmation
{
    get { return _PasswordConfirmation; }
    set { this.RaiseAndSetIfChanged(x => x.PasswordConfirmation, value); }
}

我也有一个命令,就是设置是这样的:

I also have a command that is setup like this:

var canHitOk = this.WhenAny(
    x => x.Password,
    x => x.PasswordConfirmation,
    (pass, confirm) => (pass.Value == confirm.Value && pass.Value.Length > 3));

OkCommand = new ReactiveCommand(canHitOk);

正如您可以猜到,我也有密码确认文本/属性设置也和一个按钮(密码确认文本框有同样的问题)的命令集。由于字段/属性组合都从未在视图模型更新,按钮从未启用。

As you can guess, I also have the password confirmation textbox/property setup too, and the command set for a button (The password confirmation TextBox has the same problem). Since the field/property combos are never updated in the ViewModel, the button is never enabled.

我调试并确认该视图模型绑定到XAML,但文本永远不会在字段/属性组合在文本框输入时更新。

I debugged and confirmed that the ViewModel is bound to the XAML, but the text never gets updated in the field/property combos when typing in the TextBoxes.

我看着在样本WP7应用GitHub的回购,他们手动绑定的KeyUp 事件来设置在属性/字段中的文本。如果我遵守这个约定,我有这个在我的MainWindow.xaml:

I looked at the sample WP7 application in the GitHub repo and they manually bind the KeyUp event to set the text in the text in the property/field. If I follow this convention, I have this in my MainWindow.xaml:

Password.KeyUp += (o, e) =>
{
    ViewModel.Password = Password.Text;
};

但是,这是正确的方式做到这一点?我以为无UI会处理的结合,因为我已经看到了在其他MVVM框架,除非我错了。是否有更简单的方法?

But is that the right way to do it? I thought Reactive UI would handle the binding as I've seen in other MVVM frameworks, unless I'm mistaken. Is there an easier way?

更新

正如保罗·贝茨在他回答的评论中指出的,您可以启用绑定通过增加该属性的绑定自动更新:

As Paul Betts pointed out in the comments in his answer, you can enable the binding to automatically update by adding this property to the binding:

UpdateSourceTrigger=PropertyChanged

这将使TextBox元素是这样的:

which would make the TextBox element look like this:

<TextBox Name="Password" Text="{Binding Password, UpdateSourceTrigger=PropertyChanged}" />


推荐答案

的问题是,WP7文本框不更新其上的KeyUp自动绑定 - 这是与每一个MVVM框架伤心的问题。

The problem is that WP7 TextBox doesn't Update its binding automatically on KeyUp - this is a problem with every MVVM framework sadly.

这篇关于如何设置一个视图模型绑定与ReactiveUI一个TextBox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆