清除TextBox的WPF事件触发器

时间:2018-02-14 11:55:16

标签: wpf textbox mahapps.metro

所以我有username --------

TextBox

当按下<TextBox Controls:TextBoxHelper.ClearTextButton="True" LostFocus="textboxNewValueCell_LostFocus" TextChanged="textboxNewValueCell_TextChanged"/> 按钮时,我想要抓住Clear

有可能吗?

我没找到任何event

2 个答案:

答案 0 :(得分:0)

ClearTextButton只需拨打Clear()上的TextBox即可。没有提出具体事件。您可以做的最好的事情是处理TextChanged事件:

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

答案 1 :(得分:0)

首先,为TextBox命名。然后,在Button上创建一个click事件。当click事件触发时,处理CodeBehind中TextBox的清除。

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