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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With