DataGridTextColumn双击全选

时间:2013-12-11 15:41:30

标签: c# wpf xaml datagrid

我有一个DataGridTextColumn但是当我点击进入单元格时,文本现在变得可编辑,但当我双击文本时,它不会选择所有文本(或只是当前单词)。

                    <DataGridTextColumn ClipboardContentBinding="{Binding Path=Name}" SortMemberPath="Name" 
                                        Header="Name" 
                                        Binding="{Binding Path=Name, Mode=TwoWay, UpdateSourceTrigger=Explicit}" CanUserReorder="True" CanUserSort="True" CanUserResize="True" Width="SizeToHeader" />

1 个答案:

答案 0 :(得分:1)

其中一个解决方案是为datagirdcells设置样式,为每个单元设置MouseDoubleClick事件。

<Window.Resources>
    <Style TargetType="DataGridCell">
        <EventSetter Event="MouseDoubleClick" Handler="CellDoubleClick"/>
    </Style>
</Window.Resources>

代码背后......

    /// <summary>
    /// Select all text in DataGridCell on DoubleClick
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void CellDoubleClick(object sender, RoutedEventArgs e)
    {
        DataGridCell cell = null;
        TextBox textBox = null;

        cell = sender as DataGridCell;
        if (cell == null)
        {
            return;
        }

        textBox = cell.Content as TextBox;
        if (textBox == null)
        {
            return;
        }
        textBox.SelectAll();
    }