WPF文本框中不能从视图模型更新 [英] Wpf text box not updated from view model

查看:97
本文介绍了WPF文本框中不能从视图模型更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的视图模型的属性,它返回在一定条件下的恒定。

I have a property in my view model which returns a constant under some conditions.

    class Tmp : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public String Text
        {
            get { return "testing"; }
            set
            {
                PropertyChanged(this,new PropertyChangedEventArgs("Text"));                }
        }
    }

所以属性Text alwasys返回测试。

so the property Text alwasys returns "testing".

我必将这一个文本框,如:

I bound this to a text box like :

  <TextBox Text="{Binding Path=Text, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

应用程序启动时,该文本框correclty说测试。

When the application starts, the textbox correclty says testing.

现在当我输入在文本框中setter方法​​被调用,调用的PropertyChanged东西。

now when I type something in the text box the setter gets called, which calls PropertyChanged.

这东西(可能是GUI)后调用getter和获取价值的测试。

After this something ( probably the GUI ) calls the getter and gets the value "testing".

然而,在文本框中的文本不会更改回测试。

However the text in the textbox is not changed back to testing.

所以我可以输入ABC到文本框和文本框显示ABC,尽管该模型只是存储测试。

So I can type "abc" into the text box, and text box displays "abc" even though the model is just storing "testing".

为什么心不是在文本框中的文本不重置为测试在每一个按键?

why isnt the text in the text box not reset to "testing" at each keystroke?

推荐答案

为什么要再次文本框获取文本?它只是把它写进你的源属性为知道它必须是相同的,因为他是在告诉属性的新值的中间。否则,你会创建循环引用。你在做什么是对从物业预计的准则和行为完全去。

Why should the textbox get the text again? It just wrote it into your source property it "knows" that it must be the same, because he is in the middle of telling the property the new value. Otherwise you would create circular references. What you are doing is going completely against guidelines and behavior expected from a property.

删除二传手,使结合的一种方式,提高当你不断改变文本属性,使文本框只读。请记住,它没有必要调用的PropertyChanged的制定者。

Remove the setter, make the binding one way, raise the text property when your constant is changed, and make the textbox readonly. Remember, its not necessary to call Propertychanged in the setter.

为使您最初的方法的工作,你需要摆脱的文本框改变属性,但不会听传入涨薪的状态。

To make your initial approach work, you need to break out of the "Textbox changes property but won't listen for incoming raises" state

set
{
   sUIDispatcher.BeginInvoke((Action)(() => Raise("Name")));
}

我想补充一点,这是一个非常糟糕的主意,并强烈反对你那样做。

i would like to add, that this is a REALLY bad idea and strongly discourage you to do it like that.

这篇关于WPF文本框中不能从视图模型更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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