WPF DataGridTextColumn头绑定 [英] WPF DataGridTextColumn header binding

查看:2213
本文介绍了WPF DataGridTextColumn头绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个code中,绑定不起作用。

I have a code in which Binding doesn't work

<DataGridTextColumn Header="{Binding LocalizedText.Task_Toolbar_AddButton}" />

有关按钮:

<Button x:Name="addTaskButton" Click="addTaskButton_Click">
<TextBlock Text="{Binding LocalizedText.Task_Toolbar_AddButton, Mode=OneWay}" />
</Button>

它工作正常,但对于DataGrid标题根本不工作。

it works fine, but for datagrid header doesn't work at all.

推荐答案

查找约什 - 史密斯的约DataContext的间谍,其中DataContextSpy类使用Hillberg的可冻结伎俩从一个对象,它是不是在一个逻辑树获得继承方面的博客。 DataContextSpy是很简单的,所以应该在许多情况下被重用。

Look up Josh Smith's blogs about DataContext Spy, where DataContextSpy class uses Hillberg’s Freezable trick to gain access to an inheritance context from an object that is not in a logical tree. DataContextSpy is very simple, so it should be reusable in many scenarios.

下面是如何使用它的头文件(我用这一切的时候,不仅DataGrid.Headers):

Here's how you can use it on headers (I use it all the time, not only on DataGrid.Headers):

    <DataGrid...
    <DataGrid.Resources>
        <myNamespaces:DataContextSpy x:Key="dcSpy" DataContext="{LocalizedText}"/>
         .......

    <DataGridTemplateColumn Header="{Binding Source={StaticResource dcSpy}, Path=DataContext.Task_Toolbar_AddButton}">

编辑:
我似乎无法找到它在他的博客的任何地方,也许他归档,所以在这里,我只是将它加入你。贴吧,引用它在XAML正如我上面显示,然后用它的DataContext拉出你想要的数据绑定:

I can't seem to find it anywhere on his blog, maybe he archived it, so here, I'll just add it for you. Paste it, reference it in XAML as I showed above, then use it's DataContext to pull out what the data you want to bind:

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

这篇关于WPF DataGridTextColumn头绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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