2011-02-14 50 views
6

我用CellEditingTemplate編寫了DataGrid的用戶控件。此編輯模板的DataTemplate是一個文本框,並且光標將通過點擊三次進入文本框,如果我想通過雙擊或一次單擊將文本框上的光標設置爲光標,我該怎麼辦?WPF:CellEditingTemplate如何通過雙擊或點擊將焦點設置在內部控件上

這是我的代碼:

<Window x:Class="MultiLineEditDataGrid.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:MultiLineEditDataGrid" 
    Title="MainWindow" Height="350" Width="525"> 
<Grid DataContext="{Binding Source={x:Static Application.Current}, Path=CompanyManager}"> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="270"/> 
     <RowDefinition Height="30"/> 
    </Grid.RowDefinitions> 
    <DataGrid ItemsSource="{Binding Companies}" CanUserAddRows="False" AutoGenerateColumns="False"> 
     <DataGrid.Resources> 
      <DataTemplate x:Key="cellTemplate"> 
       <TextBlock Text="{Binding Description}"/> 
      </DataTemplate> 
      <DataTemplate x:Key="cellEditingTemplate"> 
       <local:MultiLineTextBox Text="{Binding Description}"/> 
      </DataTemplate> 
     </DataGrid.Resources> 
     <DataGrid.Columns> 
      <DataGridTextColumn Header="Company" Binding="{Binding Name}"/> 
      <DataGridTemplateColumn Header="Description" 
            CellTemplate="{StaticResource cellTemplate}" 
            CellEditingTemplate="{StaticResource cellEditingTemplate}"/> 
     </DataGrid.Columns> 
    </DataGrid> 
    <Button Grid.Row="1" Content="Add" Command="{Binding AddCommand}"/> 
</Grid> 

MultiLineTextBox是i。從文本框繼承文本框,並重寫的onkeydown方法。

MultiLineTextBox代碼:

public class MultiLineTextBox : TextBox 
{ 
    /// <summary> 
    /// On Key Down. 
    /// </summary> 
    /// <param name="e"></param> 
    protected override void OnKeyDown (KeyEventArgs e) 
    { 
     base.OnKeyDown (e); 
     string oldText = Text; 
     ModifierKeys keys = Keyboard.Modifiers; 
     if (e.Key == Key.Enter) 
     { 
      if ((Keyboard.Modifiers & ModifierKeys.Control).Equals (ModifierKeys.Control)) 
      { 
       int index = SelectionStart; 
       oldText = oldText.Insert (index, Environment.NewLine); 
       Text = oldText; 
       Select (index + 1, 0); 
       e.Handled = true; 
      } 
      else 
      { 
       e.Handled = false; 
      } 
     } 
     else if (e.Key == Key.Escape) 
     { 
      Text = oldText; 
      e.Handled = false; 
     } 
    } 
} 

回答

9

我找到一個方法來解決這個問題,這裏是我的代碼。

<Window x:Class="MultiLineEditDataGrid.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:MultiLineEditDataGrid" 
    Title="MainWindow" Height="350" Width="525"> 
<Grid DataContext="{Binding Source={x:Static Application.Current}, Path=CompanyManager}"> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="270"/> 
     <RowDefinition Height="30"/> 
    </Grid.RowDefinitions> 
    <DataGrid ItemsSource="{Binding Companies}" CanUserAddRows="False" AutoGenerateColumns="False"> 
     <DataGrid.Resources> 
      <DataTemplate x:Key="cellTemplate"> 
       <TextBlock Text="{Binding Description}"/> 
      </DataTemplate> 
      <DataTemplate x:Key="cellEditingTemplate"> 
       <local:MultiLineTextBox x:Name="multiLineTxt" Text="{Binding Description}"/> 
       <DataTemplate.Triggers> 
        <Trigger SourceName="multiLineTxt" Property="IsVisible" Value="True"> 
         <Setter TargetName="multiLineTxt" Property="FocusManager.FocusedElement" Value="{Binding ElementName=multiLineTxt}"/> 
        </Trigger> 
       </DataTemplate.Triggers> 
      </DataTemplate> 
     </DataGrid.Resources> 
     <DataGrid.Columns> 
      <DataGridTextColumn Header="Company" Binding="{Binding Name}"/> 
      <DataGridTemplateColumn Header="Description" 
            CellTemplate="{StaticResource cellTemplate}" 
            CellEditingTemplate="{StaticResource cellEditingTemplate}"/> 
     </DataGrid.Columns> 
    </DataGrid> 
    <Button Grid.Row="1" Content="Add" Command="{Binding AddCommand}"/> 
</Grid> 

8

我不知道爲什麼,但以前的答案並沒有我的情況下工作。我在這裏找到了替代解決方案http://madcoderspeak.blogspot.ca/2010/04/set-keyboard-focus-when-user-begins.html

<DataGridTemplateColumn.CellEditingTemplate> 
    <DataTemplate> 
     <StackPanel> 
      <TextBox x:Name="editCommentTextBox" Text="{Binding Comment, Mode=TwoWay}" 
        FocusManager.FocusedElement="{Binding RelativeSource={RelativeSource Self}}"> 
      </TextBox> 
      <Label Content="{Binding Text, ElementName=editCommentTextBox, Converter={StaticResource CharCounterConverter}}"/> 
     </StackPanel> 
    </DataTemplate> 
</DataGridTemplateColumn.CellEditingTemplate> 
+1

比接受的答案簡單,並且對我很好。非常感謝。 – 2013-01-15 05:49:45