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

这个设计将如下所示

在此处输入图像描述

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; 
                // I want o remove the Gotfocus envet here.  
                GetInstance.Focus(); //If i comment this line cursor will not focus on textbox
                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();
    }


}

输出将如下所示

在此处输入图像描述

当我单击退格按钮时,文本框将被删除,光标将聚焦在文本框中。问题是,当我单击并按住退格按钮时,文本框的值不会重复删除。如果注释GetInstance.Focus(); 从上面的代码中,值被重复删除,但在重复删除文本框上的文本值时光标没有聚焦。

但我有一个想法,如果在 GetInstance.Focus(); 之前删除事件(txt_remove_GotFocus );,当单击并按住退格按钮时,文本框值会重复删除。之后将在rbtn_remove_LostMouseCapture事件中添加新的事件处理程序。

最后我想实现以下场景。

例如在文本框中输入值,然后单击并按住系统键盘上的退格键,您将获得差价。

如果您对上述情况有任何其他想法,请与我分享。

4

1 回答 1

1

为什么不使用RepeatButton类?

来自 MSDN 的引用:

表示一个控件,该控件从按下它到释放它之前反复引发其 Click 事件。

这是 MVVM 方式的代码示例:

1)示例视图模型:

public class ViewModel : ViewModelBase
{
    public ViewModel()
    {
        this.text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
        this.backspaceCommand = new RelayCommand(
            () => Text = Text.Substring(0, Text.Length - 1), 
            () => !String.IsNullOrEmpty(Text));
    }

    public String Text
    {
        get { return text; }
        set
        {
            if (text != value)
            {
                text = value;
                OnPropertyChanged("Text");
            }
        }
    }
    private String text;

    public RelayCommand BackspaceCommand
    {
        get { return backspaceCommand; }
    }
    private readonly RelayCommand backspaceCommand;
}

3)窗口标记:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="114" Width="404">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>

        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <TextBox Grid.Column="0" Margin="5" x:Name="tbText" Text="{Binding Text, UpdateSourceTrigger=PropertyChanged}"/>
        <RepeatButton Grid.Column="1" Margin="5" Content="Backspace" Command="{Binding BackspaceCommand}"/>
    </Grid>
</Window>

3)窗口代码隐藏:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        Loaded += (sender, args) => 
        { 
            tbText.Focus(); 
            tbText.CaretIndex = tbText.Text.Length; 
        };
        DataContext = new ViewModel();
    }
}

这是丑陋的代码隐藏示例:

更新。 此示例已更新为在按下按钮时显示插入符号。首先,我们应该禁用对按钮的关注。其次,我们应该在TextBox更改文本后修复插入符号的位置。

1) 窗口标记:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="114" Width="404">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>

        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <TextBox Grid.Column="0" Margin="5" x:Name="tbText" Text="Lorem ipsum dolor sit amet, consectetur adipiscing elit."/>
        <RepeatButton Grid.Column="1" Margin="5" Focusable="False" Content="Backspace" Click="RepeatButton_Click"/>
    </Grid>
</Window>

2)窗口代码隐藏:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        Loaded += (sender, args) => 
        { 
            tbText.Focus(); 
            tbText.CaretIndex = tbText.Text.Length; 
        };
    }

    private void RepeatButton_Click(object sender, RoutedEventArgs e)
    {
        if (!string.IsNullOrEmpty(tbText.Text))
        {
            tbText.Text = tbText.Text.Substring(0, tbText.Text.Length - 1);
            tbText.CaretIndex = tbText.Text.Length;
        }
    }
}
于 2012-08-02T05:42:51.107 回答