C# WPF 滑鼠移至TextBox,全選內容

//Mouse up event handler
private void PreviewMouseUpHandler(object sender, MouseButtonEventArgs e)
{
    TextBox tb = (sender as TextBox);

    if (tb == null)
			return;

    t.SelectAll();

    //在全選後,可能會造成畫面凍住,用ReleaseMouseCapture解決
    t.ReleaseMouseCapture();
}
//Code Behind
TextBox textBox = new TextBox();
textBox.PreviewMouseUp += PreviewMouseUpHandler;
//XAML
<TextBox x:Name="textBox" Text={Binding}  PreviewMouseUp="PreviewMouseUpHandler" />

發佈留言