基本的MVVM测试。仅在命令触发后才设置属性。 [英] Basic MVVM Test. Property is set only AFTER the Command fires.

查看:75
本文介绍了基本的MVVM测试。仅在命令触发后才设置属性。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有一个非常基本的ViewModel,它有一个属性 SearchText 和一个命令 GetPersonsCommand



Hi,

I've got a very basic ViewModel that has a property SearchText and a command GetPersonsCommand:

private string _searchText;
public string SearchText
{
    get
    {
        return _searchText;
    }
    set
    {
        if (value != this._searchText)
        {
           this._searchText = value;
           OnPropertyChanged("SearchText");
        }
    }
}
public BaseCommand GetPersonsCommand { get; set; }





在BaseCommand中没有任何奇特的事情发生,它只是ICommand的基本实现。



查看:

在我看来,我有一个搜索文本框。我创建了一个行为,它在TextChanged上有一点延迟(允许用户输入),当他们停止输入时,我的 GetPersonsCommand 被触发。这有效。



There's nothing fancy happening in BaseCommand, it's just a basic implementation of ICommand.

View:
In my view, I have a "Search" TextBox. I created a Behavior that has a slight delay on TextChanged (allowing the user to type) and when they stop typing, my GetPersonsCommand gets fired. This works.

<TextBox x:Name="txtSearch" Text="{Binding SearchText, Mode=TwoWay}">
    <i:Interaction.Behaviors>
        <b:DelayTextBox Command="{Binding GetPersonsCommand}" Interval="1000" />
    </i:Interaction.Behaviors>





这就是:TextBox中的用户类型 - >当他们停止输入GetPersonsCommand被解雇时。到目前为止很棒...



我的问题是,用户第一次键入John时,Command被触发,SearchText属性仍为空白。现在用户再次键入John Doe,SearchText属性现在为John。



为什么SearchText属性仅在我的命令被触发后才更新?在我的脑海中,当用户更改TextBox中的文本时,由于双向绑定,ViewModel的SearchText属性应该立即更新。



我在做什么错了?



这是我的行为,因为这很重要......



This is what happens: User types in TextBox -> When they stop typing GetPersonsCommand gets fired. Great so far...

My issue is that the first time the user types "John", the Command is fired, the SearchText property is still blank. Now the user types again "John Doe" and the SearchText property is now "John".

Why does the SearchText property only update itself after my Command is fired? In my head, when a user changes the text in the TextBox, because of the 2 way binding, the ViewModel's SearchText property should IMMEDIATELY be updated.

What am I doing wrong?

Here's my behavior, incase that matters....

public class DelayTextBox : Behavior<TextBox>
{
    #region Properties

    public ICommand Command
    {
        get { return (ICommand)GetValue(CommandProperty); }
        set { SetValue(CommandProperty, value); }
    }

    public static readonly DependencyProperty CommandProperty =
        DependencyProperty.Register("Command", typeof(ICommand), typeof(DelayTextBox), null);

    public double Interval
    {
        get { return (double)GetValue(IntervalProperty); }
        set { SetValue(IntervalProperty, value); }
    }

    public static readonly DependencyProperty IntervalProperty =
        DependencyProperty.Register("Interval", typeof(double), typeof(DelayTextBox), new PropertyMetadata((s, e) =>
        {
            DelayTextBox parent = (DelayTextBox)s;
            if (parent != null && parent._timer != null)
            {
                parent._timer.Stop();
                parent._timer.Interval = TimeSpan.FromMilliseconds((double)e.NewValue);
            }
        }));

    #endregion

    private DispatcherTimer _timer;
    protected override void OnAttached()
    {
        base.OnAttached();

        this.AssociatedObject.TextChanged += AssociatedObject_TextChanged;

        this._timer = new DispatcherTimer();
        this._timer.Tick += timer_Tick;
        this._timer.Interval = TimeSpan.FromMilliseconds(this.Interval);
    }

    protected override void OnDetaching()
    {
        base.OnDetaching();

        this.AssociatedObject.TextChanged -= AssociatedObject_TextChanged;
        this._timer.Tick -= timer_Tick;
    }

    private void timer_Tick(object sender, EventArgs e)
    {
        this._timer.Stop();

        if (this.Command != null)
        {
            this.Command.Execute(null);
        }
    }

    private void AssociatedObject_TextChanged(object sender, TextChangedEventArgs e)
    {
        if (this._timer.IsEnabled)
        {
            this._timer.Stop();
            this._timer.Start();
        }
        else
        {
            this._timer.Start();
        }
    }
}





提前致谢!



Thanks in advance!

推荐答案

得到了答案这里



只需要将 UpdateSourceTrigger = PropertyChanged 添加到我的绑定更新TextChange上的ViewModel属性,而不是失去焦点。 (Silverlight 5)
Got my answer here.

Just needed to add UpdateSourceTrigger=PropertyChanged to my binding to update the ViewModel property on TextChange and not on lose focus. (Silverlight 5)


这篇关于基本的MVVM测试。仅在命令触发后才设置属性。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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