Wpf DataGrid ClipboardCopyMode = "IncludeHeader" with custom header

I have a WPF table that has its own header (based on a StackPanel) that includes a button that displays and processes the setting of units for the column. Which works well, however I want to be able to copy data to the clipboard, including the 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 custom copy header to clipboard as

SomeHeader System.Windows.Controls.StackPanel
v1         33

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

+5
source share
3 answers

, , ToString(), ClipboardCopyMode="IncludeHeader" .

:

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>

/ "" System.Windows.Controls.Image. , StackPanel. ,

+6

HeaderTemplate . . , . , IncludeHeader , .

 /// <summary>  
 /// WPF Data grid does not know what is in a header template, so it can't copy it to the clipboard when using ClipboardCopyMode="IncludeHeader".  
 /// This attached property works with a header template that includes one TextBlock. Text content from the templates TextBlock is copied to the  
 /// column header for the clipboard to pick up.  
 /// </summary>  
public static class TemplatedDataGridHeaderText  
{  
 private static readonly Type OwnerType = typeof(TemplatedDataGridHeaderText);  
 public static readonly DependencyProperty UseTextFromTemplateProperty = DependencyProperty.RegisterAttached("UseTextFromTemplate", typeof(bool), OwnerType, new PropertyMetadata(false, OnHeaderTextChanged));  
 public static bool GetUseTextFromTemplate(DependencyObject obj)  
 {  
   return (bool)obj.GetValue(UseTextFromTemplateProperty);  
 }  
 public static void SetUseTextFromTemplate(DependencyObject obj, bool value)  
 {  
   obj.SetValue(UseTextFromTemplateProperty, value);  
 }  
 private static void OnHeaderTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)  
 {  
   var textColumn = d as DataGridTextColumn;  
   if (textColumn == null) return;  
   if (textColumn.HeaderTemplate == null) return;  
   var headerTemplateTexblockText = textColumn.HeaderTemplate.LoadContent().GetValue(TextBlock.TextProperty).ToString();  
   textColumn.Header = headerTemplateTexblockText;  
 }  
}  

xaml .

 <DataGrid ItemsSource="{Binding }" AutoGenerateColumns="False" IsReadOnly="True" VerticalScrollBarVisibility="Auto" VerticalAlignment="Stretch">  
 <DataGrid.Columns>  
   <DataGridTextColumn Binding="{Binding FlowRate.UserValue, StringFormat=N3}" HeaderTemplate="{StaticResource FlowRate}"  
             attachedProperties:TemplatedDataGridHeaderText.UseTextFromTemplate="True"/>  
   <DataGridTextColumn Binding="{Binding Pressure.UserValue, StringFormat=N3}" HeaderTemplate="{StaticResource Pressure}"  
             attachedProperties:TemplatedDataGridHeaderText.UseTextFromTemplate="True"/>  
 </DataGrid.Columns>  

... http://waldoscode.blogspot.com/2014/08/issue-using-wpf-datagrid-columnheader.html

+1

I used an alternative AttachedProperty in the GetFuzzy link http://waldoscode.blogspot.com/2014/08/issue-using-wpf-datagrid-columnheader.html . The author (Don) created AttachedProperty as follows (with a few small mods):

/// <summary>  
/// Allows binding a property to the header text. Works with the clipboard copy mode - IncludeHeaders.  
/// </summary>  
public static class DataGridHeaderTextAttachedProperty  
{  
  private static readonly Type OwnerType = typeof(DataGridHeaderTextAttachedProperty);  
  public static readonly DependencyProperty HeaderTextProperty = DependencyProperty.RegisterAttached("HeaderText", typeof(string), OwnerType, new PropertyMetadata(OnHeaderTextChanged));  

  public static string GetHeaderText(DependencyObject obj)  
  {  
    return (string)obj.GetValue(HeaderTextProperty);  
  }  

  public static void SetHeaderText(DependencyObject obj, string value)  
  {  
    obj.SetValue(HeaderTextProperty, value);  
  }  

  private static void OnHeaderTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)  
  {  
    var column = d as DataGridColumn;  
    if (column == null) return;  
    column.Header = GetHeaderText(column);  
  }  
}

I was not able to get it to work when setting up Column.Header directly, but it was able to get it to work with HeaderTemplate, as shown below:

<DataGridTemplateColumn ...
                        ui:DataGridHeaderTextAttachedProperty.HeaderText="Some Text">
    <DataGridTemplateColumn.HeaderTemplate>
        <DataTemplate>
            <Path Data="{StaticResource SomeGeometry}" ... />
        </DataTemplate>
    </DataGridTemplateColumn.HeaderTemplate>

    ...
</DataGridTemplateColumn>

THANKS GetFuzzy FOR EXCELLENT BLOG POST!

0
source

All Articles