0

So i have this Dictionary:

 Dictionary<string, double> _statistics;

And this is how this is looks like:

enter image description here

When I do a mouse right click I want to be able to copy the cell value. How can I add this simple copy option that will mark only the specific cell and not all the row ? Is there something that already exist in the ListView ?

Populate my ListView:

foreach (KeyValuePair<string, double> item in _statistics)
    listView.Items.Add(new MyItem { IP = item.Key, Percent = item.Value });

<ListView.View>
    <GridView ColumnHeaderContainerStyle="{StaticResource ListViewHeaderStyle}">
        <!-- ip address column -->
        <GridViewColumn Width="200" Header="IP Address" DisplayMemberBinding="{Binding IP}" />

        <!-- percent column sent -->
        <GridViewColumn Width="200" Header="Percent" DisplayMemberBinding="{Binding Percent, StringFormat={}{0}%}"/>
    </GridView>
</ListView.View>

public class MyItem
{
    public string IP { get; set; }
    public double Percent { get; set; }
}
Emmanuel DURIN
  • 4,803
  • 2
  • 28
  • 53
mark yer
  • 403
  • 6
  • 12

2 Answers2

0

I think you can catch the mouse click event at the list view level, and check the source element is a TextBlock. If so, set the clipboard text to the text block's value.

Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
0

GridViewColumn.CellTemplate is the key. Define custom data template for your cell using CellTemplate of the GridViewColumn, and create a ContextMenu in there.

AgentFire
  • 8,944
  • 8
  • 43
  • 90
  • @markyer sure, [This question](http://stackoverflow.com/questions/11101360/textblock-text-wrapping-in-gridviewcolumn-celltemplate-not-working) contains an example for you. – AgentFire Sep 27 '15 at 10:56