在非主要TabItem中时,绑定在DataGrid列标题中不起作用 [英] Binding not working in DataGrid column header when inside non-primary TabItem

查看:50
本文介绍了在非主要TabItem中时,绑定在DataGrid列标题中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a DataGrid which has a bound UI control in its header. The binding is to a a field in a view model. The grid is inside a TabControl. When the grid is on the initial tab in the tab control, all works well.

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

However, when the grid isn't on the initial tab, no binding seems to take place. There's no console output, value converters aren't being invoked, trace level logging isn't reporting anything.

这里有一个简单的,可重现的示例:

Here's a trivial, reproducible example:

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



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



MainWindow.xaml.cs

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


推荐答案

之后试图解决您的问题几分钟而没有成功,我找到了一个在线解决方案,似乎可以解决您的问题。

After trying to fix your issue for a few minutes without success, i've found a solution online which seems to fix your issue.

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

我已经尝试过了,并且可以使用!

I've tried it and it works! Good luck!

这里有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));
}

这篇关于在非主要TabItem中时,绑定在DataGrid列标题中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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