在非主要TabItem中时,绑定在DataGrid列标题中不起作用

时间:2018-09-22 21:34:47

标签: wpf xaml wpfdatagrid tabcontrol

我有一个DataGrid,它的标头中有一个绑定的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();
    }
}

1 个答案:

答案 0 :(得分:1)

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

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>

还有绑定代理类:

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