WPF Datagrid clipboard get cell value










0















I need to handle the Ctrl+C function in my datagrid, especially I need to get the cell of a selected value. Here's the XAML code of the datagrid. Please note that I use 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>


The code behind to handle the copy:



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;




Basically my BoundPropName returns "Field.Formula.Formula" instead of the value visible in the cell of datagrid. How can I get the cell value?










share|improve this question






















  • Firstly, i believe the default copy function of DataGrid should work as expected. Is there any specific reason that you're handling the Copy function? Also, in the command executed handler you're trying to get the Binding path alone. This will not give you Cell value; try accessing the SelectedCell to get the value.

    – dhilmathy
    Nov 15 '18 at 12:45











  • Hi, I've implemented this custon copy function because on Win7 if I use Ctrl+C I get an Exception error (this is due to several reason, ie .NET framework version).

    – John Goodman
    Nov 15 '18 at 13:31















0















I need to handle the Ctrl+C function in my datagrid, especially I need to get the cell of a selected value. Here's the XAML code of the datagrid. Please note that I use 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>


The code behind to handle the copy:



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;




Basically my BoundPropName returns "Field.Formula.Formula" instead of the value visible in the cell of datagrid. How can I get the cell value?










share|improve this question






















  • Firstly, i believe the default copy function of DataGrid should work as expected. Is there any specific reason that you're handling the Copy function? Also, in the command executed handler you're trying to get the Binding path alone. This will not give you Cell value; try accessing the SelectedCell to get the value.

    – dhilmathy
    Nov 15 '18 at 12:45











  • Hi, I've implemented this custon copy function because on Win7 if I use Ctrl+C I get an Exception error (this is due to several reason, ie .NET framework version).

    – John Goodman
    Nov 15 '18 at 13:31













0












0








0








I need to handle the Ctrl+C function in my datagrid, especially I need to get the cell of a selected value. Here's the XAML code of the datagrid. Please note that I use 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>


The code behind to handle the copy:



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;




Basically my BoundPropName returns "Field.Formula.Formula" instead of the value visible in the cell of datagrid. How can I get the cell value?










share|improve this question














I need to handle the Ctrl+C function in my datagrid, especially I need to get the cell of a selected value. Here's the XAML code of the datagrid. Please note that I use 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>


The code behind to handle the copy:



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;




Basically my BoundPropName returns "Field.Formula.Formula" instead of the value visible in the cell of datagrid. How can I get the cell value?







wpf datagrid clipboard






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 12:08









John GoodmanJohn Goodman

247




247












  • Firstly, i believe the default copy function of DataGrid should work as expected. Is there any specific reason that you're handling the Copy function? Also, in the command executed handler you're trying to get the Binding path alone. This will not give you Cell value; try accessing the SelectedCell to get the value.

    – dhilmathy
    Nov 15 '18 at 12:45











  • Hi, I've implemented this custon copy function because on Win7 if I use Ctrl+C I get an Exception error (this is due to several reason, ie .NET framework version).

    – John Goodman
    Nov 15 '18 at 13:31

















  • Firstly, i believe the default copy function of DataGrid should work as expected. Is there any specific reason that you're handling the Copy function? Also, in the command executed handler you're trying to get the Binding path alone. This will not give you Cell value; try accessing the SelectedCell to get the value.

    – dhilmathy
    Nov 15 '18 at 12:45











  • Hi, I've implemented this custon copy function because on Win7 if I use Ctrl+C I get an Exception error (this is due to several reason, ie .NET framework version).

    – John Goodman
    Nov 15 '18 at 13:31
















Firstly, i believe the default copy function of DataGrid should work as expected. Is there any specific reason that you're handling the Copy function? Also, in the command executed handler you're trying to get the Binding path alone. This will not give you Cell value; try accessing the SelectedCell to get the value.

– dhilmathy
Nov 15 '18 at 12:45





Firstly, i believe the default copy function of DataGrid should work as expected. Is there any specific reason that you're handling the Copy function? Also, in the command executed handler you're trying to get the Binding path alone. This will not give you Cell value; try accessing the SelectedCell to get the value.

– dhilmathy
Nov 15 '18 at 12:45













Hi, I've implemented this custon copy function because on Win7 if I use Ctrl+C I get an Exception error (this is due to several reason, ie .NET framework version).

– John Goodman
Nov 15 '18 at 13:31





Hi, I've implemented this custon copy function because on Win7 if I use Ctrl+C I get an Exception error (this is due to several reason, ie .NET framework version).

– John Goodman
Nov 15 '18 at 13:31












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53319182%2fwpf-datagrid-clipboard-get-cell-value%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53319182%2fwpf-datagrid-clipboard-get-cell-value%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

Museum of Modern and Contemporary Art of Trento and Rovereto