【问题标题】:DagatridColumn header not properly boundDagatridColumn 标题未正确绑定
【发布时间】:2019-12-24 12:23:30
【问题描述】:

将属性绑定到 DataGrid 的标头时,我收到此错误:

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.DataGrid', AncestorLevel='1''. BindingExpression:Path=DataContext.Strings.Address; DataItem=null; target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')

代码:

<DataGridTextColumn
    MinWidth="80"
    Binding="{Binding Address, Converter={StaticResource AddressToStringConverter}}"
    IsReadOnly="True">
    <DataGridTextColumn.Header>
        <TextBlock Text="{Binding DataContext.Strings.Address, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" />
    </DataGridTextColumn.Header>
</DataGridTextColumn>

然后我想,让我们使用 snoop 来检查绑定中实际发生的情况。但是当我窥探TextBlock时,绑定突然起作用了。

我尝试绑定到上面UserControl 的DataContext,结果相同。

有人知道为什么会这样吗?

提前致谢

编辑:示例项目:

在搜索了更多之后,我发现问题是 TabControl 是父级(参见下面的代码)。我还没有任何解决方案,还很艰难...

<Window
    x:Class="TestApp.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:testapp="clr-namespace:TestApp">

    <Window.DataContext>
        <testapp:MainViewModel />
    </Window.DataContext>

    <TabControl>
        <TabItem Content="Breaks it" Header="Before" />
        <TabItem Header="Test">
            <DataGrid AutoGenerateColumns="False" ItemsSource="{Binding People}">
                <DataGrid.Columns>
                    <DataGridTextColumn Binding="{Binding Name}">
                        <DataGridTextColumn.Header>
                            <TextBlock Text="{Binding DataContext.Name, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}" />
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                </DataGrid.Columns>
            </DataGrid>
        </TabItem>
        <TabItem Content="Doesn't break it" Header="After" />
    </TabControl>
</Window>

public partial class MainWindow 
{
    public MainWindow()
    {
        InitializeComponent();
    }
}

public class MainViewModel
{
    public string Name => "Name";
    public ObservableCollection<Person> People { get; } = new ObservableCollection<Person>
    {
        new Person{Name="Mickey Mouse"},
    };
}

public class Person : INotifyPropertyChanged
{
    private string _name;
    public string Name
    {
        get => _name;
        set
        {
            _name = value;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Name)));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

当DataGrid 不在第一个选项卡中时,绑定似乎不起作用。

解决方案?

【问题讨论】:

  • DataGrid 是如何定义的?请同时显示绑定到DataContext 的对象

标签: c# wpf data-binding datagrid


【解决方案1】:

DataGridTextColumn 的 DataContext 将成为您收藏的 Address 属性。如果您尝试绑定到页面/控件DataContext 的属性,则需要找到父控件并绑定到它的DataContext,例如:

<DataGridTextColumn.Header>
        <TextBlock Text="{Binding DataContext.Strings.Address, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Grid}}}" />
</DataGridTextColumn.Header>

这可行,但如果您需要绑定多个项目,代码会变得非常不清楚,并且当出现问题时很难调试。为了简化从不同的DataContext 中访问我的主要DataContext,我现在喜欢做的是在 XAML 本身中设置它:

<Page 
    xmlns:models="clr-namespace:CoolApp1.Models"
   ...>
        <Page.DataContext>
             <models:MyModel x:Name="Context"/>
        </Page.DataContext>

然后你可以这样做:

<DataGridTextColumn.Header>
        <TextBlock Content="{Binding ElementName=Context, Path=Strings.Address}"/>
</DataGridTextColumn.Header>

我希望我理解了这个问题,这对你有帮助!

【讨论】:

  • 您好,感谢您的回复。我不绑定到 DataGridTextColumn 的 DataContext,我绑定到 DataGrid。这与父控件具有相同的 DataContext,因为它是自动传递的。 (我也尝试引用 UserControls DataContext。)除此之外,如果绑定不正确,当我使用 snoop 进行检查时它也不应该工作,但它突然工作了。
  • 嗯,我很难说出发生了什么。涉及到几个因素。首先是 DataGrid 的 DataContext。另一个是您尝试绑定的属性是否实现通知...其他是通知触发器和绑定模式...也许您可以先尝试在绑定中将 NotifyOnSourceUpdated 更改为 true?看看有没有帮助
  • 去过那里,做到了。所有的东西都设置好了(据我所知)。这些类都实现了 INotifyPropertyChanged,我一直在玩触发器,itemsSource 已正确绑定(因为我确实正确获取了所有行),......唯一不能正常工作的是标题
  • 让我直截了当地说...您想将DataGridTextColumn 中的TextBox.Text 绑定到您的DataGrid 的DataContext.Strings.Address ... 对吗?如果是这样,您的 DataGrid 必须从具有 String.Address 属性的父元素(我认为是您的用户控件/页面)继承 DataContext。到那里还好吗?
  • 我想将DataGridTextColumn 的标题绑定到DataContext.Strings.Address 的值(最终使用工具提示,这就是我使用TextBlock 的原因)。 DataContext 包含所需的属性并且绑定正确,因为我确实在 DataGrid 中获取数据并且所有字段都是可见的。但是,标题都是空的,因为它找不到父元素的相对源 (Data Error: 4)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-12-05
  • 2016-12-28
  • 1970-01-01
  • 2018-05-18
  • 2014-02-05
  • 2013-08-11
  • 2023-01-29
相关资源
最近更新 更多