簡體   English   中英

清除文本框的 WPF 事件觸發器

[英]WPF event trigger for clear TextBox

所以我有TextBox

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

當按下Clear按鈕時,我想捕捉event

是否可以 ?

我沒有找到任何event

ClearTextButton只是在TextBox上調用Clear() 沒有引發特定事件。 您能做的最好的事情是處理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...
    }
}

首先,為您的 TextBox 命名。 然后,在按鈕上創建一個單擊事件。 當點擊事件觸發時,處理 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;
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM