Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wpf DataGrid ClipboardCopyMode="IncludeHeader" with custom header

I have a WPF table that has a custom header (based on a StackPanel) which includes a button that displays and handles setting the units of for the column. Which works nicely, however I want to be able to copy the data to clipboard including headers.

<DataGrid ClipboardCopyMode="IncludeHeader"
...
<DataGridTextColumn Header="Some Header" Binding={Binding Path=SomeValue}/>
<DataGridTextColumn Binding={Binding Path=OtherValue, Converter="{StaticResource unitsConverter}">
<DataGridTextColumn.Header>
<StackPanel>
<TextBlock Text="Period" />
<Button ... />
</Stackpanel>

The problem is that columns with the custom header copy to the clipboard as

SomeHeader System.Windows.Controls.StackPanel
v1         33

Is there a way to change what text is printed for the header when a custom header is used?

like image 321
metalmonkey Avatar asked Feb 19 '13 02:02

metalmonkey


1 Answers

I poked around for a solution then ended up subclassing my custom header control just to override the ToString() so that ClipboardCopyMode="IncludeHeader" would copy the correct text.

In my case I used an image in my header:

class HeaderImage : Image
{
    public override string ToString()
    {
        return Tag.ToString();
    }
}

Xaml:

 <DataGridCheckBoxColumn.Header>
     <elements:HeaderImage Source="..\Resources\skull.png" Width="15" Tag="Deprecated"/>
 </DataGridCheckBoxColumn.Header>

Now the copy/paste data has "Deprecated" instead of System.Windows.Controls.Image. I'm sure you could do the same with the StackPanel. I used Tag as header text since it was convenient

like image 158
xerous Avatar answered Sep 28 '22 08:09

xerous