在 non-primary TabItem 中时,绑定在 DataGrid 列 header 中不起作用

Binding not working in DataGrid column header when inside non-primary TabItem

我有一个 DataGrid,它的 header 中有一个绑定 UI 控件。绑定是到视图模型中的一个字段。网格在 TabControl 内。当网格位于选项卡控件的初始选项卡上时,一切正常。

但是,当网格不在初始选项卡上时,似乎没有绑定发生。没有控制台输出,没有调用值转换器,跟踪级别日志记录没有报告任何内容。

这是一个简单的、可重现的例子:

查看模型

public class ViewModel : INotifyPropertyChanged
{
    private string _camel = "Bertie";

    public string Camel
    {
        get => _camel;
        set
        {
            if (value == _camel) return;
            _camel = value;
            OnPropertyChanged();
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

MainWindow.xaml

<Window x:Class="ThingsWithHumps.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:thingsWithHumps="clr-namespace:ThingsWithHumps"
        mc:Ignorable="d"
        Height="350" Width="350">
    <Window.DataContext>
        <thingsWithHumps:ViewModel/>
    </Window.DataContext>
    <TabControl>
        <!-- This TabItem prevents the TextBlock being bound -->
        <TabItem Header="Troublesome tab"/>
        <TabItem Header="Humps">
            <DataGrid AutoGenerateColumns="False">
                <DataGrid.Columns>
                    <DataGridTemplateColumn>
                        <DataGridTemplateColumn.Header>

                            <!-- Troublesome binding -->
                            <TextBlock Text="{Binding Path=DataContext.Camel,
                                   UpdateSourceTrigger=PropertyChanged,
                                   RelativeSource={RelativeSource FindAncestor,
                                       AncestorType={x:Type Window}}}" />
                        </DataGridTemplateColumn.Header>
                    </DataGridTemplateColumn>
                </DataGrid.Columns>
            </DataGrid>
        </TabItem>
    </TabControl>
</Window>

MainWindow.xaml.cs

namespace ThingsWithHumps
{
    public partial class MainWindow 
    {
        public MainWindow() => InitializeComponent();
    }
}

在尝试解决您的问题几分钟但没有成功后,我在网上找到了一个似乎可以解决您的问题的解决方案。

http://www.thomaslevesque.com/2011/03/21/wpf-how-to-bind-to-data-when-the-datacontext-is-not-inherited/

我试过了,很有效!祝你好运!

这是 xaml 代码

<TabControl>
    <!-- This TabItem prevents the TextBlock being bound -->
    <TabItem Header="Troublesome tab"/>
    <TabItem Header="Humps">
        <DataGrid AutoGenerateColumns="False">
            <DataGrid.Resources>
                <local:BindingProxy x:Key="proxy" Data="{Binding}" />
            </DataGrid.Resources>
            <DataGrid.Columns>
                <DataGridTemplateColumn>

                    <DataGridTemplateColumn.Header>

                        <!-- Troublesome binding -->
                        <TextBlock Text="{Binding Path=Data.Camel,
                               UpdateSourceTrigger=PropertyChanged,
                               Source={StaticResource proxy}}" />
                    </DataGridTemplateColumn.Header>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
    </TabItem>
</TabControl>

和绑定代理 class :

public class BindingProxy : Freezable
{
    #region Overrides of Freezable

    protected override Freezable CreateInstanceCore()
    {
        return new BindingProxy();
    }

    #endregion

    public object Data
    {
        get { return (object)GetValue(DataProperty); }
        set { SetValue(DataProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Data.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty DataProperty =
        DependencyProperty.Register("Data", typeof(object), typeof(BindingProxy), new UIPropertyMetadata(null));
}