7

我有一个 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

使用自定义标题时,有没有办法更改为标题打印的文本?

4

3 回答 3

9

我四处寻找解决方案,然后最终将我的自定义标题控件子类化,只是为了覆盖它,ToString()以便ClipboardCopyMode="IncludeHeader"复制正确的文本。

就我而言,我在标题中使用了图像:

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

xml:

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

现在复制/粘贴数据已“弃用”而不是System.Windows.Controls.Image. 我相信你可以对StackPanel. 我使用标签作为标题文本,因为它很方便

于 2013-06-05T12:50:04.383 回答
1

使用其中包含文本块的 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

于 2014-08-29T22:12:14.483 回答
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 的精彩博文!

于 2015-12-09T16:18:21.190 回答