2012-08-01 24 views
0
<Grid x:Name="LayoutRoot"> 
    <TextBox x:Name="txt_remove" Height="46" Margin="234,119,225,0" TextWrapping="Wrap" VerticalAlignment="Top" GotFocus="txt_remove_GotFocus"/> 
    <RepeatButton x:Name="rbtn_remove" Content="Remove" Delay="500" Interval="100" Margin="283.667,183,282.333,222" RenderTransformOrigin="0.667,0.854" Click="rbtn_remove_Click" />     
</Grid> 

代碼在C#問題的RepeatButton在WPF

public partial class Repeate : Window 
{ 
    Control GetTextbox; 

    public Repeate() 
    { 
     this.InitializeComponent(); 

    } 

    private void rbtn_remove_Click(object sender, RoutedEventArgs e) 
    { 

     TextBox GetInstance = GetTextbox as TextBox; 

     if (GetTextbox != null) 
     { 

      string _CurrentValue = GetInstance.Text; 
      var _CareIndex = GetInstance.CaretIndex; 

      if (_CareIndex > 0) 
      { 
       string _Backspace = _CurrentValue.Remove(_CareIndex - 1, 1); 
       GetInstance.Text = _Backspace; 
       GetInstance.Focus(); 
       GetInstance.CaretIndex = _CareIndex - 1; 
      } 
     } 
    } 

    private void txt_remove_GotFocus(object sender, RoutedEventArgs e) 
    { 
     GetTextbox = (Control)sender; 
    } 

} 

有了上面的代碼,我可以得到下面的結果。

RepeatButton

如果我點擊刪除按鈕,文本框的值clear.But如果我點擊並按住刪除按鈕,它是不重複刪除文本框的值。

回答

2

一切都足以做你想做的工作。但Getinstance上的focus()方法調用。正在做歧視。

只是刪除。

GetInstance.Focus(); 

將工作。

+0

這是可以的。但是當我重複刪除文本框的值時,光標應該是可見的。我應該爲此做些什麼。專注於實例的 – 2012-08-01 09:11:22

+0

將改變重放按鈕的行爲,因爲這會鬆動活動焦點。當你從按鈕釋放焦點時,你可以做的是聚焦文本框 – JSJ 2012-08-01 09:46:03