2012-08-01 24 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();