2013-02-19 133 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一個優秀的博客文章!