【问题标题】:Bind a WPF datagrid column header to property in code behind将 WPF 数据网格列标题绑定到后面代码中的属性
【发布时间】:2015-07-31 09:03:23
【问题描述】:

谁能告诉我这是否可能。我有一个 WPF 数据网格,我想将数据网格的列标题绑定到后面代码中的属性/字段。

这就是我尝试过的。这是我的专栏代码。

<DataGridTextColumn  Header="{Binding ElementName=frmAssetPPM, Path=HeaderProperty}" 

这就是我添加到窗口 xaml 中的内容。

<Window .... Name="frmAssetPPM">

这是我在后面代码中的属性定义:

private const string HeaderPropertyConstant = "Property";
private string _headerProperty = HeaderPropertyConstant;

public string HeaderProperty 
{
    get { return _headerProperty; }
    set { _headerProperty = value; }
}

但是,当我运行应用程序时,我会在 VS 的“输出”窗口中看到此错误消息。

System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=HeaderProperty; DataItem=null; target element is 'DataGridTextColumn' (HashCode=47624635); target property is 'Header' (type 'Object')

谁能告诉我我做错了什么?或者如果我什至可以做到这一点?我在某处读到,列是一个单独的对象,这有时会导致复杂化。

【问题讨论】:

标签: c# wpf xaml binding datagrid


【解决方案1】:

有些东西很难绑定,因为它们不是可视树的一部分,例如弹出窗口或在您的情况下 - 数据网格标题。一个众所周知的解决方法是使用 Josh Smith 的 DataContextSpy。基本上,您将其实例化为资源并为其提供绑定。然后在其他地方使用该实例,您可以在其中利用它的数据上下文。网上有很多例子,但为了让你开始,这样的事情应该可以工作..

<DataGrid.Resources>
    <DataContextSpy x:Key="dcSpy" 
                    DataContext="{Binding  ElementName=frmAssetPPM, Path=HeaderProperty}"/>
 ....

那么您的绑定将起作用:

<DataGridTextColumn  Header="{Binding Source={StaticResource dcSpy}, Path=DataContext}" 

这里是 Josh Smith 的代码,如果您在网上找不到的话:

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

【讨论】:

  • 谢谢,我试过了,但它不起作用。我已将 Windows 数据上下文设置为 ViewModel。你认为这就是它不起作用的原因吗?
  • 好的,那么设置 DataContext 的 DataContext="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext}" 肯定可以工作。除非您在加载 xaml 后在窗口上设置 datacontext.. 那么这是一个时间问题。
  • 你确定吗?我可以看到我们正在将 dcSpy 的数据上下文设置为 elementname = frmAssetPPM 的数据上下文。在 XAML 中,我将 Windows 数据上下文设置为 ViewModel。 &lt;Window.DataContext&gt; &lt;Binding Source="{StaticResource assetPPM2ViewModel}"&gt;&lt;/Binding&gt; &lt;/Window.DataContext&gt;
  • 哦,等等...您将 Headers 绑定到的属性在 View 上,而不是在 ViewModel 上,让我更新答案..
  • 已更新...再试一次。现在绑定应该从视图的 HeaderProperty 中提取,而不是从 DataContext(这是您的 ViewModel)
猜你喜欢
  • 2015-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-27
  • 2014-08-25
  • 2011-02-15
相关资源
最近更新 更多