如何清除单击MVVM文本框中 [英] How to clear text box on click in MVVM

查看:196
本文介绍了如何清除单击MVVM文本框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本框和一个按钮,我想清除按钮单击文本框的内容。我使用MVVM棱镜。

I have a textbox and a button I want to clear the contents of textbox on button click. I am using MVVM prism.

我的 XAML

  <TextBox  Grid.Row="0" Text="{Binding 
       Path=TextProperty,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}" Name="txtUserEntry2"/>

   <Button Content="Select" 
       Command="{Binding Path=MyCommand}" />

和在我的视图模型

    public string TextProperty
    {

        get
        {
            return selectedText;
        }
        set
        {

            selectedText = value;

            SetProperty(ref selectedText, value);
        }
    }

    //////.........

    private void MyCommandExecuted(object obj)
    {
        TextProperty= string.Empty;
        MessageBox.Show("Command Executed");
    }



不过,这并不清除文本框。我缺少什么?

But it does not clear the textbox. What am I missing ?

推荐答案

它,因为你的二传手要设置现场两次,一次兵不血刃的PropertyChanged和其他与射击的PropertyChanged,在第二盘的SetProperty 将提高的PropertyChanged 仅当有一个新的价值,但你已经设置外地的一些值,因此组通过的SetProperty 将永远不会提高PropertyChanged的,因为你将它设置为相同的值。

Its because in your setter you are setting the field twice, one without firing PropertyChanged and the other with firing PropertyChanged , in the second set SetProperty will raise PropertyChanged only if there is a new value, but you already set the field to some value so the set through the SetProperty will never raise PropertyChanged because you are setting it to the same value.

因此,在您的setter,你应该删除:

So in your setter you should remove:

selectedText = value;

这篇关于如何清除单击MVVM文本框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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