WPF 数据网格标题文本绑定 [英] WPF datagrid header text binding

查看:30
本文介绍了WPF 数据网格标题文本绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某种原因,DataGrid 的列标题不是 FrameWork 元素,因此您不能使用绑定来设置标题文本之类的内容.如果 .NET 4.0 发生了变化,请纠正我(我现在使用的是 CodePlex 的最新 WPFToolkit).

The column header of the DataGrid is not a FrameWork element for some reason, and so you cannot use bindings to set things like the header text. Please correct me if that is wrong of if that has changed with .NET 4.0 (I am using the latest WPFToolkit from CodePlex now).

我正在尝试使用 DataGrid 进行时间表演示,其中当天的日期应该是标题文本的一部分(即Sun, Nov 01"),并且我的 XAML 中有以下内容:

I am trying to use the DataGrid for a time sheet presentation where the day's date should be part of the header text (ie, "Sun, Nov 01"), and I have the following in my XAML:

        <dg:DataGrid.Columns>
        <dg:DataGridTextColumn Header="Description" Width="Auto" Binding="{Binding Description}" IsReadOnly="True"/>
        <dg:DataGridTextColumn Header="Mon" Width="50" Binding="{Binding Allocations[0].Amount}"  />
... every other day of the week ....
        <dg:DataGridTextColumn Header="Sun" Width="50" Binding="{Binding Allocations[6].Amount}"  />
        <dg:DataGridTextColumn Header="Total" MinWidth="50" Binding="{Binding TotalAllocatedAmount}" IsReadOnly="True" />
    </dg:DataGrid.Columns>

我想使用与数据相同的 AllocationViewModel(即{Binding Allocations[0].Amount}"并将其 DisplayName 属性绑定到标题文本.有人可以告诉我怎么做吗?如果我必须使用静态资源,如何在其中获取 DataContext?

I'd like to use the same AllocationViewModel I am using for data (ie, "{Binding Allocations[0].Amount}" and bind it's DisplayName property to the header text. Can someone show me how to do that? If I have to use a static resource, how can I get the DataContext in there?

编辑----------------首选的解决方法

EDIT ---------------- PREFERRED WORK-AROUND

Josh Smith 发布了关于 DataContextSpy 不久前,它是我遇到这个问题的最干净的解决方法.这是使其工作的类:

Josh Smith had posted about a DataContextSpy awhile back, and it is the cleanest workaround I have come across to this problem. Here is the class that makes it work:

/// <summary>
/// Workaround to enable <see cref="DataContext"/> bindings in situations where the DataContext is not redily available. 
/// </summary>
/// <remarks>http://blogs.infragistics.com/blogs/josh_smith/archive/2008/06/26/data-binding-the-isvisible-property-of-contextualtabgroup.aspx</remarks>
public class DataContextSpy : Freezable
{
    public DataContextSpy()
    {
        // This binding allows the spy to inherit a DataContext.
        BindingOperations.SetBinding(this, DataContextProperty, new Binding());
    }

    public object DataContext
    {
        get { return GetValue(DataContextProperty); }
        set { SetValue(DataContextProperty, value); }
    }

    // Borrow the DataContext dependency property from FrameworkElement.
    public static readonly DependencyProperty DataContextProperty = FrameworkElement
        .DataContextProperty.AddOwner(typeof (DataContextSpy));

    protected override Freezable CreateInstanceCore()
    {
        // We are required to override this abstract method.
        throw new NotImplementedException();
    }
}

有了这个,我可以在 xaml 中劫持我需要的 DC:

With this in place, I can hijack the DC I need in xaml:

    <dg:DataGrid.Resources>
        <behavior:DataContextSpy x:Key="spy" DataContext="{Binding Allocations}" />
    </dg:DataGrid.Resources>

然后根据需要通过绑定应用:

And then apply as needed via binding:

            <dg:DataGridTextColumn Header="{Binding Source={StaticResource spy}, Path=DataContext[0].DisplayName}" 
                               Width="50" Binding="{Binding Allocations[0].Amount}"  />

呸!

推荐答案

这是将 DataGridTextColumn 标题绑定到数据上下文的简单方法:

This is the easy way to bind the DataGridTextColumn header to the data context:

<DataGrid x:Name="summaryGrid" Grid.Row="3" AutoGenerateColumns="False" IsReadOnly="True" CanUserAddRows="False">
       <DataGrid.Columns>
            <DataGridTextColumn Header="Hard Coded Title" Width="*"/>
            <DataGridTextColumn Width="100">
                <DataGridTextColumn.Header>
                    <TextBlock Text="{Binding DataContext.SecondColumnTitle, 
                                              RelativeSource={RelativeSource AncestorType={x:Type local:MainWindow}}}"/>
                </DataGridTextColumn.Header>
            </DataGridTextColumn>
            <DataGridTextColumn Width="150">
                <DataGridTextColumn.Header>
                    <TextBlock Text="{Binding DataContext.ThirdColumnTitle, 
                                              RelativeSource={RelativeSource AncestorType={x:Type local:MainWindow}}}"/>
                </DataGridTextColumn.Header>
            </DataGridTextColumn>
        </DataGrid.Columns>
    </DataGrid>

您显然需要在您的数据上下文类中实现以下属性:SecondColumnTitleThirdColumnTitle.

You will obviously need to have properties: SecondColumnTitle and ThirdColumnTitle implemented on your data context class.

我在 .net 4.5 中使用了这个解决方案,并且没有机会也没有理由在框架的早期版本中尝试它.

I have this solution working in .net 4.5 and did not have a chance nor reason to try it in earlier versions of the framework.

这篇关于WPF 数据网格标题文本绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆