我有一个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
当使用自定义页眉时,是否有方法更改页眉打印的文本?
我四处寻找解决方案,然后最终将我的自定义头控件子类化,只是为了覆盖ToString()
,以便ClipboardCopyMode="IncludeHeader"
复制正确的文本。
在我的例子中,我在标题中使用了一个图像:
class HeaderImage : Image
{
public override string ToString()
{
return Tag.ToString();
}
}
Xaml:
<DataGridCheckBoxColumn.Header>
<elements:HeaderImage Source="..Resourcesskull.png" Width="15" Tag="Deprecated"/>
</DataGridCheckBoxColumn.Header>
现在,复制/粘贴数据已被"弃用",而不是System.Windows.Controls.Image
。我相信你也可以用StackPanel
。我使用Tag作为标题文本,因为它很方便
当我使用一个有文本块的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
我在GetFuzzy的链接中使用了替代AttachedPropertyhttp://waldoscode.blogspot.com/2014/08/issue-using-wpf-datagrid-columnheader.html.作者(Don)创建了一个AttachedProperty,如下所示(带有我自己的几个小MOD):
/// <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的精彩博客帖子!