2012-08-01 43 views
2
<Grid x:Name="BackSpaceButton">  
    <TextBox x:Name="txt_remove" Height="46" Margin="234,119,225,0" TextWrapping="Wrap" VerticalAlignment="Top" GotFocus="txt_remove_GotFocus" TabIndex="2"/>   
    <RepeatButton x:Name="rbtn_remove" Content="Backspace" Delay="400" Interval="200" Margin="415,124,0,0" RenderTransformOrigin="0.667,0.854" Click="rbtn_remove_Click" LostMouseCapture="rbtn_remove_LostMouseCapture" HorizontalAlignment="Left" Height="41" VerticalAlignment="Top" Width="66" TabIndex="2" />   
</Grid> 

這樣的設計會像下面如何點擊文本框中的光標點擊wpf中的RepeatButton?

enter image description here

public partial class Repeate : Window 
{ 
    Control GetTextbox; 
    TextBox GetInstance; 
    public Repeate() 
    { 
     this.InitializeComponent(); 
    } 

    private void rbtn_remove_Click(object sender, RoutedEventArgs e) 
    { 

     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.CaretIndex = _CareIndex - 1; 
      } 
     } 
    } 

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

    private void rbtn_remove_LostMouseCapture(object sender, MouseEventArgs e) 
    { 
     GetInstance.Focus(); 
    } 


} 

出認沽就會像下面

enter image description here

當我點擊退格鍵文本框將刪除光標將重點放在文本框中。問題在於,當我點擊並按住Backspa時ce按鈕反覆刪除文本框的值,但光標未顯示。

對於例如:在文本框中輸入值,然後單擊並按住系統鍵盤上的退格鍵,然後您將收取差額費用。

回答

2

使用下面的代碼它會幫助你well.why因爲有一天我面臨同樣的問題。

<Grid x:Name="BackSpaceButton"> 
    <TextBox x:Name="txt_remove" Height="46" Margin="234,119,225,0" TextWrapping="Wrap" VerticalAlignment="Top" GotFocus="txt_remove_GotFocus" TabIndex="2"/>   
    <RepeatButton x:Name="rbtn_remove" Focusable="False" Content="Backspace" Delay="400" Interval="100" Margin="415,123,0,0" RenderTransformOrigin="0.667,0.854" Click="rbtn_remove_Click" LostMouseCapture="rbtn_remove_LostMouseCapture" HorizontalAlignment="Left" Height="41" VerticalAlignment="Top" Width="66"/>   
</Grid> 
在上面的代碼

我只需添加一個屬性調焦= 「假」

private void rbtn_remove_Click(object sender, RoutedEventArgs e) 
    { 
     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;     

      } 
     } 
    } 
    void txt_remove_GotFocus(object sender, RoutedEventArgs e) 
    { 
     GetTextbox = (Control)sender; 
    } 
    private void rbtn_remove_LostMouseCapture(object sender, MouseEventArgs e) 
    { 
     GetInstance.Focus(); 
    } 
在上面的代碼

我剛加入單碼GetInstance.Focus();