使用转换器直接绑定到 DataContext 时的 WPF 更新绑定

发布于 2024-08-29 14:54:01 字数 716 浏览 5 评论 0原文

通常,当您希望数据绑定控件“更新”时,您可以使用“PropertyChanged”事件向界面发出信号,表明数据已在后台发生更改。

例如,您可以有一个通过属性“DisplayText”绑定到数据上下文的文本块

从这里开始,如果 DataContext 引发PropertyName 为“DisplayText”的 PropertyChanged 事件,则该文本块的文本应该更新(假设您没有更改绑定的模式)。

但是,我有一个更复杂的绑定,它使用数据上下文的许多属性来确定控件的最终外观和感觉。为了实现这一点,我直接绑定到数据上下文并使用转换器。在本例中,我正在使用图像源。

如您所见,我使用没有路径的 {Binding} 直接绑定到数据上下文,并且我使用一个 ImageConverter 来选择我正在寻找的图像。但现在我无法(据我所知)告诉该绑定进行更新。我尝试用“.”引发 propertychanged 事件。作为属性名称,这不起作用。

这可能吗?我是否必须将转换逻辑包装到绑定可以附加的属性中,或者是否有办法告诉绑定刷新(无需显式刷新绑定)?

任何帮助将不胜感激。 谢谢! -亚当

Normally when you want a databound control to 'update,' you use the "PropertyChanged" event to signal to the interface that the data has changed behind the scenes.

For instance, you could have a textblock that is bound to the datacontext with a property "DisplayText"

<TextBlock Text="{Binding Path=DisplayText}"/>

From here, if the DataContext raises the PropertyChanged event with PropertyName "DisplayText," then this textblock's text should update (assuming you didn't change the Mode of the binding).

However, I have a more complicated binding that uses many properties off of the datacontext to determine the final look and feel of the control. To accomplish this, I bind directly to the datacontext and use a converter. In this case I am working with an image source.

<Image Source="{Binding Converter={StaticResource ImageConverter}}"/>

As you can see, I use a {Binding} with no path to bind directly to the datacontext, and I use an ImageConverter to select the image I'm looking for. But now I have no way (that I know of) to tell that binding to update. I tried raising the propertychanged event with "." as the propertyname, which did not work.

Is this possible? Do I have to wrap up the converting logic into a property that the binding can attach to, or is there a way to tell the binding to refresh (without explicitly refreshing the binding)?

Any help would be greatly appreciated.
Thanks!
-Adam

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

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

发布评论

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

评论(3

冷︶言冷语的世界 2024-09-05 14:54:01

这里的解决方法是向我的对象添加一个名为“Self”的属性(用作数据上下文),它只是返回

public Object Self { get { return this; } 然后

然后在绑定中我使用了这个属性:

当我调用

< code>PropertyChanged(this, new PropertyChangedEventArgs("Self"))

它就像一个魅力。

谢谢大家。

The workaround here was to add a property to my object (to be used as the datacontext) called "Self" , which simply returned

public Object Self { get { return this; }}

Then in the binding I used this property:

<Image Source="{Binding Path=Self, Converter={StaticResource ImageConverter}}"/>

Then when I call

PropertyChanged(this, new PropertyChangedEventArgs("Self"))

it works like a charm.

Thanks all.

不回头走下去 2024-09-05 14:54:01

我不认为有一种方法可以用当前的转换器完全满足您的需求。正如您所提到的,您可以在 ViewModel 中进行计算,或者可以将转换器更改为 IMulitValueConverter

从您的具体场景(与 ViewModel 类及其一些属性绑定的转换器)来看,我倾向于在 ViewModel 中实现逻辑。

I don't believe there is a way of accomplishing exactly what you need with your current converter. As you mentioned, you could do the calculation in your ViewModel, or you could change your converter into an IMulitValueConverter.

From your specific scenario (the converter tied to a ViewModel class, and a few of its properties), I would lean towards implementing the logic in the ViewModel.

禾厶谷欠 2024-09-05 14:54:01

嗯,你没有显示完整的实现。但我认为如果绑定到 GUI 的值提供 PropertyChanged-Event,它应该更新。

问候

Hmm, you don't show the full implementation. But I think it should update, if the value bound to the GUI provides the PropertyChanged-Event.

Regards

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