在wpf中的RepeatButton中出现问题

时间:2012-08-01 08:09:41

标签: c# wpf repeatbutton

<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#

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

如果我点击删除按钮,则文本框值清除。但是,如果我单击并按住删除按钮,则不会重复删除文本框值

1 个答案:

答案 0 :(得分:2)

一切都足以完成你想做的工作。但是focus()方法调用了Getinstance。正在进行扩散。

删除。

GetInstance.Focus();

会奏效。

相关问题