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 ();の前にevent(txt_remove_GotFocus)を削除するかどうかはわかります。、Backspaceボタンをクリックして押したままにすると、テキストボックスの値が繰り返し削除されます。その後、rbtn_remove_LostMouseCaptureenventに新しいイベントハンドラーが追加されます。

最後に、私は以下のシナリオを達成したいと思います。

例:テキストボックスに値を入力し、システムキーボードからバックスペースキーをクリックして押したままにすると、差額が請求されます。

上記のシナリオで他のアイデアがあれば、私と共有してください。

4

1 に答える 1

1

RepeatButtonクラスを使用してみませんか?

MSDNからの引用:

押されてから離されるまで、Clickイベントを繰り返し発生させるコントロールを表します。

MVVM-wayコードサンプルは次のとおりです。

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)Windowsマークアップ:

<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 に答える