简体   繁体   中英

WPF event trigger for clear TextBox

So i have the TextBox :

<TextBox Controls:TextBoxHelper.ClearTextButton="True"
         LostFocus="textboxNewValueCell_LostFocus"
         TextChanged="textboxNewValueCell_TextChanged"/>

And when press on Clear button i want to catch the event .

Is it possible ?

I did not find any event

The ClearTextButton simply calls Clear() on the TextBox . There is no specific event raised. The best you can do is to handle the TextChanged event:

private void textboxNewValueCell_TextChanged(object sender, TextChangedEventArgs e)
{
    TextBox tb = sender as TextBox;
    if (tb.Text.Length == 0)
    {
        //the TextBox was cleared and the Button was maybe clicked...
    }
}

First, give your TextBox a name. Then, create a click event on the Button. when the click event fires, handle the clearing of the TextBox in the CodeBehind.

<Grid>
    <TextBox x:Name="MyTextBox" Text="Some Text"/>
    <Button x:Name="ClearButton" Click="ClearButton_Click"/>
</Grid>


 private void ClearButton_Click(object sender, RoutedEventArgs e)
 {
     MyTextBox.Text = string.Empty;
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM