WPF Datagrid剪贴板获取单元格值

时间:2018-11-15 12:08:39

标签: wpf datagrid clipboard

我需要在我的数据网格中处理Ctrl + C函数,尤其是我需要获取选定值的单元格。这是数据网格的XAML代码。请注意,我使用 DataGridTextColumn

<DataGrid x:Name="GridFormule" Grid.Row="0" BorderBrush="#abadb3" CanUserSortColumns="true" Sorting="GridFormule_Sorting" MaxColumnWidth="Infinity" Style="{DynamicResource DataGridStyle}" ColumnHeaderStyle="{DynamicResource DataGridColumnHeaderStyle}" ItemsSource="{Binding ElementName=ObjectsTree, Path=SelectedItem.Infos}" CellEditEnding="GridFormule_CellEditEnding" Margin="8,5,2,0" ContextMenu="{StaticResource cntextListe}">
    <!-- OVERRIDE COPY CONTROL -->
    <DataGrid.InputBindings>
        <KeyBinding Key="C" Modifiers="Control" Command="Copy" />
    </DataGrid.InputBindings>
    <DataGrid.CommandBindings>
        <CommandBinding Command="Copy" Executed="Comandi_Executed" />
    </DataGrid.CommandBindings>
    <!-- /OVERRIDE COPY CONTROL -->
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding Field.Formula.Formula, Mode=TwoWay}" Header="@_57_Formule" Foreground="Black" Width="*" ClipboardContentBinding="{Binding Field.Formula.Formula, Mode=TwoWay}">
            <DataGridTextColumn.ElementStyle>
                <Style TargetType="{x:Type TextBlock}">
                    <Setter Property="Padding" Value="4" />
                    <Setter Property="FontWeight" Value="Normal" />
                    <Setter Property="FontSize" Value="11.55" />
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding IsDirty}" Value="True">
                            <Setter Property="TextBlock.Background" Value="{StaticResource IsDirtyColor}" />
                            <Setter Property="Padding" Value="4" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </DataGridTextColumn.ElementStyle>
            <DataGridTextColumn.EditingElementStyle>
                <Style TargetType="TextBox">
                    <Setter Property="Background" Value="White"></Setter>
                    <Setter Property="Padding" Value="2,4,2,3"></Setter>
                    <Setter Property="BorderThickness" Value="0"></Setter>
                </Style>
            </DataGridTextColumn.EditingElementStyle>
        </DataGridTextColumn>
        </DataGridTextColumn>
    </DataGrid.Columns>
</DataGrid>     

处理副本的代码:

private void Comandi_Executed(object sender, ExecutedRoutedEventArgs e)
{
    RoutedUICommand c = (RoutedUICommand)e.Command;
    switch (c.Name)
    {
        // Copia
        case "Copy":
            DataGrid sel = (sender as DataGrid);
            if (sel == null) return;
            if (sel.CurrentItem != null) return;

            var cc = sel.CurrentColumn;
            Binding binding = (Binding)cc;

            // Here I get the property Name!
            string BoundPropName = binding.Path.Path;                    

            try
            {
                //Clipboard.SetDataObject(text);
            }
            catch (Exception ex)
            {
                Global.LOG.Log(ex.Message);
            }
            break;
    }
}       

基本上,我的BoundPropName返回“ Field.Formula.Formula”,而不是在datagrid单元格中可见的值。如何获取单元格值?

0 个答案:

没有答案
相关问题