在WPF中,为什么在绑定限制的位置不做模板绑定工作?

发布于 2025-01-21 08:01:07 字数 1221 浏览 3 评论 0原文

好的...这让我挠头。我有两个WPF控件 - 一个是一个用户控件,另一个是自定义控件。让我们称它们为userfoo和customfoo。在CustomFoo的控制模板中,我使用了一个userfoo的实例,该实例是命名零件,以便在应用模板后可以获取它。效果很好。

现在,userfoo和customfoo都有text在它们上定义的属性(即独立于使用addowner的共享dp。不要问...),它们都是这样声明的

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

...该模式设置为twoway,并将更新设置为“再次为属性交换”。

因此,在CustomFoo的样式模板中,我想将CustomFoo的文本属性绑定为内部Userfoo的文本属性的源。通常,这很容易。您只需将Userfoo的文本属性设置为“ {templateBinding text}”,但由于某种原因,它仅采用一种方式(即从CustomFoo中正确设置了UserFoo,但不是反向设置),即使再次设置了两个DPS,都设置为双向!但是,当使用相对源绑定而不是模板绑定时,它效果很好!嗯...哇??

// 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}"

那给什么呢?我想念什么?

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.

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤云独去闲 2025-01-28 08:01:07

在MSDN上找到了该论坛的帖子:

>

是模板场景的结合形式的优化形式,类似于构建的结合

{Binding RelativeSource={RelativeSource TemplatedParent}}

op的注释:与文档中所说的相反,实际上应该是...

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

我对文档提出了投诉,虽然他们确实添加它们始终是单向的,代码示例仍然没有列出模式,但我想它总比没有好。)

templatebinding将数据从模板父母传输到模板绑定的属性。如果您需要以相反的方向或两种方式传输数据,请使用模式属性将模式属性设置为OnewayTosource或Twoway创建一个绑定。

更多信息: http://msdn.microsoft.com/en-en-us/library/ MS742882.aspx

看起来像 mode = Oneway 是使用templatebinding 的“优化”之一

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

It says this:

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

{Binding RelativeSource={RelativeSource TemplatedParent}}

Note from OP: Contrary to what it says in the documentation, in actuality, it should be this...

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

I filed a complaint against the docs, and while they did add a sentence now stating they are always one-way, the code example still doesn't list the mode, but I guess it's better than nothing.)

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.

More in: http://msdn.microsoft.com/en-us/library/ms742882.aspx

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

对风讲故事 2025-01-28 08:01:07

模板键入不支持双向绑定,只有绑定才能做到这一点。即使使用您的BindstWowayDefault选项,它也不支持双向绑定。

可以找到更多信息。

但是,模板键入只能
向一个方向传输数据:从
元素的模板父母
使用模板绑定。如果需要的话
在相反的情况下传输数据
方向或两种方式,与
模板父母的亲属是
您唯一的选择。例如,
与文本框或滑块的互动
在模板中只会改变
模板父母上的属性
您使用双向绑定。

TemplateBinding does not support two-way binding, only Binding does that. Even with your BindsTwoWayBeDefault option, it won't support two-way binding.

More info can be found here, but to summarize:

However, a TemplateBinding can only
transfer data in one direction: from
the templated parent to the element
with the TemplateBinding. If you need
to transfer data in the opposite
direction or both ways, a Binding with
RelativeSource of TemplatedParent is
your only option. For example,
interaction with a TextBox or Slider
within a template will only change a
property on the templated parent if
you use a two-way Binding.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文