从DataGrid单元格复制文本

Kap*_*íko 6 c# wpf datagrid mvvm

我希望能够从DataGrid Cell复制文本.

  1. 第一种可能的解决方案可能是设置SelectionUnit,Cell但这不是我的选择,因为我需要选择FullRow
  2. 第二种可能的方法是在DataGridTemplateColumn其中使用readonly TextBox.但是样式存在问题.我之前的问题:DatagridCell样式被TextBox样式覆盖.我需要非常明亮的颜色用于行中的文本,但在选定的行中确实很暗.
  3. 三是设置DataGrid的IsReadOnly ="假",并提供EditingElementStyleDataGridTextColumn

    <Style x:Key="EditingStyle" TargetType="{x:Type TextBox}">
    <Setter Property="IsReadOnly" Value="True"/>
    </Style>
    ...  
    <DataGridTextColumn ... EditingElementStyle="{DynamicResource EditingStyle}"/>
    
    Run Code Online (Sandbox Code Playgroud)

    但是这里出现了一个非常可怕的错误WPF Datagrid文本列允许在内部文本框设置为只读时输入一个字符文本.

你知道一些不同的解决方案吗?或解决方法?谢谢.

编辑

我注意到,DataGrid扩展WPF工具包没有这个错误,但它似乎有不同的结构,我不能应用我的DataGrid样式.

我注意到使用ReadOnly TextBox作为DataGridColumn的EditingElementStyle会带来更多问题.当您使用OneWay绑定时,不可能将单元格设置为编辑状态.让用户覆盖DataGrid中显示的某个实体的ID,这是不可接受的.所以它必须以某种方式只读或至少OneWay绑定.

此刻我完全无法解决这个问题.在选择并突出显示行时,是否有其他方法让用户从单元格复制?我没有注意到其他一些解决方案吗?谢谢阅读.

CSh*_*ark 2

你可以做一些肮脏的事情来获取当前的单元格。在你的xaml中只需添加

<DataGrid GotFocus="DataGrid_GotFocus" KeyDown="DataGrid_KeyDown">
Run Code Online (Sandbox Code Playgroud)

并在代码隐藏中

private void DataGrid_GotFocus(object sender, RoutedEventArgs e)
{
    if(e.OriginalSource is DataGridCell)
        _currentCell = (DataGridCell) e.OriginalSource;
}

private void DataGrid_KeyDown(object sender, KeyEventArgs e)
{
    if(e.Key == Key.C && (e.SystemKey == Key.LeftCtrl || e.SystemKey == Key.RightCtrl))
    {
         //Transform content here, like
         Clipboard.SetText(_currentCell.Content);
    }
}
Run Code Online (Sandbox Code Playgroud)

这应该可以做到,因为GotFocus每次数据网格本身中的选择发生更改时都会执行。