2012-08-02 69 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

當我點擊退格鍵文本框將刪除光標將集中在文本框中。問題是,當我單擊並按住Backspace時b在不重複刪除文本框中的值。如果發表評論GetInstance.Focus();從上面的代碼中重複刪除值,但是在重複刪除文本框上的文本值時光標未對焦。

但我有一個想法,如果刪除事件(txt_remove_GotFocus)之前GetInstance.Focus();,當單擊並按住Backspace按鈕時,文本框值將重複刪除。然後將在rbtn_remove_LostMouseCapture envent中添加新的事件處理程序。

最後我想實現下面的場景。

對於例如:在文本框中輸入值,然後單擊並按住系統鍵盤上的退格鍵,然後您將收取差額費用。

如果您有任何其他想法意味着以上情況,請與我分享。

回答

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

這裏有難看的代碼示例:

UPDATE。 當按下按鈕時,此示例已更新以顯示插入符號。 第一,我們應該禁用對焦按鈕。第二,我們應該修改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; 
     } 
    } 
} 
+0

請給我code.I不知道如何處理的RepeatButton類。 – 2012-08-02 05:46:36

+0

@ASHOKA,我已經更新了答案。 – Dennis 2012-08-02 06:16:39

+0

我試過了你的代碼(醜陋的代碼後面的示例)。當我點擊並按住Backspace按鈕意味着,它反覆刪除文本框vlue,但在重複刪除文本框值時不可見光標。我需要的是遊標當點擊並按住Backspace按鈕時應該可見。請清除我的問題。 – 2012-08-02 07:24:42