1

I have a textbox in which I am handling its text changed event.Now when I click button I want to clear the text from the textbox.

Now when I have text in the textbox and when I call my command the text is not cleared.

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>

ViewModel

   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;
    }
Rohit
  • 10,056
  • 7
  • 50
  • 82

2 Answers2

1

You seem not to understand the framework you are using

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

These two lines of code should/could NEVER ever be in the same block of code EVER.

What is happening is this.

The first line sets TypedText to value. OKAY...

Second line, check if TypedText is equal to value (spoiler alert, it is), and set them to be equal if not AND THEN TELL WPF that you changed to value.

The problem is, the second line never runs its logic (of tell WPF that I've changed). The reason this never runs is the first line.

Remove TypedText = value; from your code and it might just work.

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

However, one last thing. I really really really hate code where the setter DOES stuff. Why is there logic here? From an external user, it might do something unexpected.

Aron
  • 15,464
  • 3
  • 31
  • 64
  • So where should I place that code ? any link any example ? i was referring http://stackoverflow.com/questions/55855/textbox-textchanged-icommandsource – Rohit Apr 08 '15 at 08:23
  • @kyle I am sorry to tell you, but you completely misunderstood the code being referenced. `SetProperty` replaces those two lines of code (intelligently I might add). But of the two lines you were "copying" you only copied ONE of them. – Aron Apr 08 '15 at 08:27
0

I have a textbox in which I am handling its text changed event

No you don't, or at least not in the code excerpt that you have shown in your question:

<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>

In this example, you have a string property data bound to the TextBox.Text property, which is similar, but not the same as handling its text changed event.

Either way, in order to clear this data bound value, you just need to set your data bound string property to an empty string (after removing that extraneous code from the setter):

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

...

private void MyCommandExecuted(string text)
{
    SearchText = string.Empty;
}
Sheridan
  • 68,826
  • 24
  • 143
  • 183
  • Great detailed explanation of your problem there. What's not working exactly? Did you edit your property? Is your command being called? Where is the XAML for your command that clears the text? – Sheridan Apr 08 '15 at 08:09
  • I edited my property as you described but the text doesn't gets cleared from the textbox – Rohit Apr 08 '15 at 08:10