Wpf DataGrid的ClipboardCopyMode="IncludeHeader"与自定义标题

8

我有一个WPF表格,其中自定义了头部(基于StackPanel),其中包括一个按钮,显示并处理列的单位设置。这很好地工作,但是我希望能够将数据复制到剪贴板中,包括标题。

<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>

问题在于带有自定义表头的列会被复制到剪贴板中作为
SomeHeader System.Windows.Controls.StackPanel
v1         33

有没有办法在使用自定义标题时更改打印的标题文本?
3个回答

10

我搜索了解决方案,最终选择子类化我的自定义标题控件,以便覆盖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>

现在,复制/粘贴数据中使用的是"Deprecated",而不是System.Windows.Controls.Image。我相信你可以对StackPanel做同样的操作。我使用Tag作为标题文本,因为它很方便。


感谢@xerous,是的,它确实也适用于StackPanel。 - Sylvia

1
我在使用包含文本块的HeaderTemplate时遇到了问题,于是我通过使用附加属性解决了这个问题。可以看到,我只是从HeaderTemplate中获取文本,并将其设置到header属性中。这样剪贴板复制模式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


0

我在GetFuzzy的链接http://waldoscode.blogspot.com/2014/08/issue-using-wpf-datagrid-columnheader.html中使用了替代的AttachedProperty。作者(Don)创建了一个AttachedProperty,如下所示(加入了一些小修改):

/// <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);  
  }  
}

当直接设置Column.Header时,我无法使其正常工作,但是通过以下的HeaderTemplate方式可以使其正常工作:

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

    ...
</DataGridTemplateColumn>

感谢 GetFuzzy 发布的优秀博客文章!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接