2012-08-02 5 views
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> 

이 디자인은 내가 제거 백 스페이스 버튼의 텍스트 상자에 커서를 클릭하면 풋 아웃동적으로 wpf에서 이벤트를 추가하고 제거하는 방법은 무엇입니까?

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; 
       // 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(); 
    } 


} 

enter image description here

아래

처럼 아래처럼 될 것이다 텍스트 상자에 집중할 것입니다. 문제는 백 스페이스 b를 클릭 한 상태에서 utton은 반복적으로 제거하지 않는 텍스트 상자 값입니다. 주석이있는 경우 GetInstance.Focus(); 위의 코드에서 값은 반복적으로 제거되지만 커서는 텍스트 상자에서 텍스트 값을 반복적으로 제거 할 때 초점을 맞추지 않습니다.

하지만 이벤트 (txt_remove_GotFocus)를 제거하기 전에 아이디어가 있습니다. GetInstance.Focus();이면 Backspace 버튼을 클릭 한 상태에서 텍스트 상자 값이 반복적으로 제거됩니다. 그런 다음 rbtn_remove_LostMouseCapture envent에 새 이벤트 처리기를 추가합니다.

마지막으로 아래 시나리오를 달성하고 싶습니다.

예 : 텍스트 상자에 값을 입력하고 시스템 키보드의 백 스페이스 키를 누른 채로 있으면 차이가 있습니다.

위의 시나리오에 대한 다른 아이디어가 있다면 나와 공유하십시오.

답변

1

RepeatButton 클래스를 사용하지 않는 이유는 무엇입니까? MSDN에서

표창 :

이 출시 될 때까지 누르면됩니다 시간에서 반복적으로 클릭 이벤트를 발생시키는 컨트롤을 나타냅니다.

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) 창 코드 숨김 :

여기

은 MVVM 방향 코드 샘플입니다
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; 
     } 
    } 
} 
+0

PLS 나에게주는 code.I 해달라고 RepeatButton 클래스를 처리하는 방법을 알고있다. –

+0

@ASHOKA, 답변을 업데이트했습니다. – Dennis

+0

나는 코드 (못생긴 코드 - 방법 - 방법 샘플)을 시도했다. 내가 클릭하고 백스 페이스 버튼을 개최 의미, 그것은 반복적으로 텍스트 상자의 vlue을 제거하지만 반복적으로 텍스트 상자 values.What 내가 필요로 커서가 표시되지 않습니다 iclick 및 백스 페이스 버튼을 누르고있을 때 표시되어야합니다. 제발 내 문제를 해결하십시오. –

관련 문제