2013-02-19 40 views
5

Tôi có bảng WPF có tiêu đề tùy chỉnh (dựa trên StackPanel) bao gồm nút hiển thị và xử lý việc thiết lập đơn vị cho cột. Mà làm việc độc đáo, tuy nhiên tôi muốn để có thể sao chép dữ liệu vào clipboard bao gồm cả tiêu đề.Wpf DataGrid ClipboardCopyMode = "IncludeHeader" với tiêu đề tùy chỉnh

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

Vấn đề là cột với tiêu đề sao chép tùy chỉnh vào clipboard như

SomeHeader System.Windows.Controls.StackPanel 
v1   33 

Có cách nào để thay đổi những gì văn bản được in ra cho tiêu đề khi một tiêu đề tùy chỉnh được sử dụng?

Trả lời

6

Tôi poked xung quanh cho một giải pháp sau đó kết thúc lên subclassing kiểm soát tiêu đề tùy chỉnh của tôi chỉ để ghi đè lên ToString() để ClipboardCopyMode="IncludeHeader" sẽ sao chép văn bản chính xác.

Trong trường hợp của tôi, tôi đã sử dụng một hình ảnh trong đầu tôi:

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> 

Bây giờ các dữ liệu sao chép/dán đã "Phản đối" thay vì System.Windows.Controls.Image. Tôi chắc rằng bạn có thể làm tương tự với StackPanel. Tôi đã sử dụng Thẻ làm văn bản tiêu đề vì nó thuận tiện

+0

Cảm ơn @xerous và có nó thực sự làm việc cho một StackPanel là tốt. –

1

Tôi đang tìm giải pháp cho vấn đề này khi sử dụng HeaderTemplate có khối văn bản trong đó. Trong trường hợp của tôi, tôi đã giải quyết vấn đề với thuộc tính đính kèm. Bạn có thể thấy rằng tôi chỉ lấy văn bản từ mẫu tiêu đề và đặt nó vào thuộc tính tiêu đề. Cách thức sao chép clipboard này bao gồm chế độ IncludeHeader hoạt động như mong đợi.

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

Các XAML sẽ trông như thế này ....

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

Thông tin thêm có thể được tìm thấy ở đây ... http://waldoscode.blogspot.com/2014/08/issue-using-wpf-datagrid-columnheader.html

0

tôi đã sử dụng AttachedProperty thay thế trong liên kết GetFuzzy của http://waldoscode.blogspot.com/2014/08/issue-using-wpf-datagrid-columnheader.html. Tác giả (Don) đã tạo ra một AttachedProperty như sau (với một vài mods nhỏ của riêng tôi):

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

tôi đã không thể có được nó để làm việc khi thiết lập các Column.Header trực tiếp nhưng đã có thể làm cho nó hoạt động với HeaderTemplate như dưới đây:

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

    ... 
</DataGridTemplateColumn> 

THANKS MỌI LẠI CHO MỘT BÀI VIẾT BỔ SUNG!

Các vấn đề liên quan