【问题标题】:How to clear textbox on click button [duplicate]如何清除单击按钮上的文本框[重复]
【发布时间】:2015-06-13 00:20:25
【问题描述】:

我有一个文本框,我在其中处理其文本更改事件。现在,当我单击按钮时,我想从文本框中清除文本。

现在当我在文本框中有文本并且当我调用我的命令时,文本不会被清除。

xaml

   <TextBox   Text="{Binding SearchText,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}" Name="mytxtBox">
        <TextBox.InputBindings>
            <KeyBinding Command="{Binding Path=SearchCommand}" CommandParameter="{Binding ElementName=mytxtBox, Path=Text}" Key="Enter"/>
        </TextBox.InputBindings>
    </TextBox>

视图模型

   public string SearchText
    {

        get
        {
            return TypedText;
        }
        set
        {
             TypedText=value;
                if (string.IsNullOrEmpty(TypedText.ToString()))// This is called when the text is empty
                {
                    Some Logic//
                }             
            SetProperty(ref TypedText, value);   
        }
    }


    private void MyCommandExecuted(string text)
    {
        SearchText= string.Empty;
    }

【问题讨论】:

  • SetProperty 的代码是什么?它会引发属性更改通知吗? ,您需要为 SearchText 提升属性更改
  • 是的,它来自 Prism,它会发出通知
  • 但它如何知道要为 SearchText 发出通知?如果可以,请发布代码
  • 你的 searchtext 属性设置器不应该有那么多逻辑。只需检查新值并引发属性更改事件
  • 将 RaisePropertyChanged("SearchText") 添加到设置器

标签: c# wpf mvvm prism


【解决方案1】:

您似乎不了解您正在使用的框架

public string SearchText
{
    set 
    { 
         TypedText = value; 
         SetProperty(ref TypedText, value); 
    } 
}

这两行代码应该/永远不能在同一个代码块中。

这是怎么回事。

第一行将TypedText 设置为value。好的...

第二行,检查 TypedText 是否等于 value(剧透警告,确实如此),如果不相等,则将它们设置为相等,然后告诉 WPF 您已更改为 value。

问题是,第二行永远不会运行它的逻辑(告诉 WPF 我已经改变了)。这永远不会运行的原因是第一行。

从您的代码中删除TypedText = value;,它可能会正常工作。

    set
    {
        if (string.IsNullOrEmpty(value))// This is called when the text is empty
        {
            Some Logic//
        }             
        SetProperty(ref TypedText, value);   
    }

但是,最后一件事。我真的真的真的很讨厌二传手做事的代码。为什么这里有逻辑?从外部用户那里,它可能会做一些意想不到的事情。

【讨论】:

  • 那么我应该把代码放在哪里?任何链接任何例子?我指的是stackoverflow.com/questions/55855/…
  • @kyle 很抱歉告诉你,但你完全误解了所引用的代码。 SetProperty 替换了这两行代码(我可能会智能地添加)。但是在您“复制”的两行中,您只复制了其中之一。
【解决方案2】:

我有一个文本框,我在其中处理它的文本更改事件

不,您没有,或者至少在您在问题中显示的代码摘录中没有:

<TextBox Text="{Binding SearchText, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Name="mytxtBox">
    <TextBox.InputBindings>
        <KeyBinding Command="{Binding Path=SearchCommand}" CommandParameter="{Binding ElementName=mytxtBox, Path=Text}" Key="Enter"/>
    </TextBox.InputBindings>
</TextBox>

在本例中,您有一个字符串属性数据绑定到TextBox.Text 属性,这与处理其文本更改事件类似,但又不同。

无论哪种方式,为了清除此数据绑定值,您只需将数据绑定字符串属性设置为空字符串(在从设置器中删除该无关代码之后):

public string SearchText
{
    get { return TypedText; }
    set { TypedText = value; SetProperty(ref TypedText, value); } 
}

...

private void MyCommandExecuted(string text)
{
    SearchText = string.Empty;
}

【讨论】:

  • 对您的问题的详细解释。什么不正确?您是否编辑了您的财产?你的命令被调用了吗?用于清除文本的命令的 XAML 在哪里?
  • 我按照您的描述编辑了我的属性,但文本没有从文本框中清除
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-20
  • 1970-01-01
  • 1970-01-01
  • 2013-03-15
相关资源
最近更新 更多