在WPF中,为什么不TemplateBinding工作在哪里绑定呢? [英] In WPF, why doesn't TemplateBinding work where Binding does?

查看:192
本文介绍了在WPF中,为什么不TemplateBinding工作在哪里绑定呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧......这是留给我抓我的头。我有两个WPF控件 - 一个人的用户控制和对方的自定义控件。让我们把他们UserFoo和CustomFoo。在CustomFoo控制模板,我用UserFoo的实例,这是一个名为一部分,所以在应用模板后,我可以得到它。这工作正常。

Ok... this is leaving me scratching my head. I have two WPF controls--one's a user control and the other's a custom control. Let's call them UserFoo and CustomFoo. In the control template for CustomFoo, I use an instance of UserFoo which is a named part so I can get to it after the template is applied. That works fine.

现在UserFoo和CustomFoo都有一个上定义文本属性(独立,即不共享的DP使用AddOwner。不要问...),它们都声明如下...

Now both UserFoo and CustomFoo have a Text property defined on them (independently, i.e. not a shared DP using AddOwner. Don't ask...) that are both declared like this...

public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
    "Text",
    typeof(string),
    typeof(UserFoo), // The other is CustomFoo
    new FrameworkPropertyMetadata(
        null,
        FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
        null,
        null,
        true,
        UpdateSourceTrigger.PropertyChanged
    )
);

通知具体的模式被设定为双向和UpdateSourceTrigger设置为的PropertyChanged,再次对两个

Notice specifically that the mode is set to TwoWay and the UpdateSourceTrigger is set to PropertyChanged, again for both.

所以在CustomFoo样式模板,我想CustomFoo的Text属性绑定作为源到内部UserFoo的Text属性。通常情况下,这是很容易。您只需设置UserFoo的text属性为{TemplateBinding文字},但由于某种原因,它一定会一种方式(即USerFoo被正确地从CustomFoo设置,但不是相反),即使再次,无论是DPS们进行双向设置!然而,使用相对源代码而不是绑定模板结合时,它的伟大工程!嗯...世界卫生大会?

So in the style template for CustomFoo, I want to bind CustomFoo's Text property as the source to the internal UserFoo's Text property. Normally, this is easy. You just set UserFoo's text property to "{TemplateBinding Text}" but for some reason it's only going one way (i.e. USerFoo is properly set from CustomFoo, but not the reverse), even though again, both DPs are set for two-way! However, when using a relative source binding instead of a template binding, it works great! Um... wha??

// This one works
Text="{Binding Text, RelativeSource={RelativeSource AncestorType={local:CustomFoo}}, Mode=TwoWay}"

// As does this too...
Text="{Binding Text, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"

// But not this one!
Text="{TemplateBinding Text}"

那么怎么办?我缺少什么?

So what gives? What am I missing?

推荐答案

在发现这个MSDN论坛的帖子:<一href=\"http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/0bb3858c-30d6-4c3d-93bd-35ad0bb36bb4/\">http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/0bb3858c-30d6-4c3d-93bd-35ad0bb36bb4/

Found this forum post on MSDN: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/0bb3858c-30d6-4c3d-93bd-35ad0bb36bb4/

它这样说:

一个TemplateBinding是绑定模板场景,类似于绑定构建了

A TemplateBinding is an optimized form of a Binding for template scenarios, analogous to a Binding constructed with

{Binding RelativeSource={RelativeSource TemplatedParent}}.

从OP注:相反的是它的文件中称,实际上,那它应该是...

{Binding RelativeSource={RelativeSource TemplatedParent} Mode=OneWay}. 

我提出了对文档的投诉,虽然他们做了补充一句,现在说明他们始终是单向的,在code例子还没有列出的模式,但我想这是不是更好什么都没有。)

从模板父母是模板绑定的属性的TemplateBinding传输数据。如果你需要在相反的方向,或两种方式传输数据,创建具有TemplatedParent的一个的RelativeSource绑定的模式属性设置为OneWayToSource或双向。

The TemplateBinding transfers data from the templated parent to the property that is template bound. If you need to transfer data in the opposite direction or both ways, create a Binding with RelativeSource of TemplatedParent with the Mode property set to OneWayToSource or TwoWay.

在更多:<一href=\"http://msdn.microsoft.com/en-us/library/ms742882.aspx\">http://msdn.microsoft.com/en-us/library/ms742882.aspx

看起来像的模式=单向的是使用TemplateBinding的优化的一个

Looks like Mode=OneWay is one of the "Optimizations" of using a TemplateBinding

这篇关于在WPF中,为什么不TemplateBinding工作在哪里绑定呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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