2013-02-19 132 views
5

我有一个WPF表有一个自定义标题(基于StackPanel),其中包含一个按钮来显示和处理设置列的单位。这很好,但我希望能够将数据复制到剪贴板,包括标题。Wpf DataGrid ClipboardCopyMode =“IncludeHeader”与自定义标题

<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 

列有没有办法来改变打印文本什么的头当使用自定义标题?

回答

6

我捅围绕一个解决方案,然后结束了继承我的自定义标题控制只覆盖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做同样的事情。我使用标签作为标题文本,因为它很方便

+0

谢谢@xerous,是的它确实也适用于StackPanel。 – 2013-09-09 14:21:13

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

0

我使用的替代AttachedProperty在GetFuzzy的链接http://waldoscode.blogspot.com/2014/08/issue-using-wpf-datagrid-columnheader.html。笔者(唐)创建为遵循AttachedProperty(一对夫妇我自己的小的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); 
    } 
} 

我是不是能够得到它直接设置Column.Header时工作,但能得到它与HeaderTemplate中如下工作:

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

    ... 
</DataGridTemplateColumn> 

感谢GetFuzzy一个优秀的博客文章!