6

我用 CellEditingTemplate 写了一个 DataGrid 的用户控件。这个编辑模板的DataTemplate是一个TextBox,点击三下光标会进入文本框,如果我想通过双击或单击将光标设置在文本框上怎么办?

这是我的代码:

<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 是我从文本框继承的文本框,并覆盖 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;
        }
    }
}
4

2 回答 2

11

我不知道为什么,但以前的答案对我来说不起作用。我在这里找到了替代解决方案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>
于 2012-11-01T21:27:21.643 回答
10

我找到了解决这个问题的方法,这是我的代码。

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

于 2011-02-14T06:12:55.017 回答