WPF绑定,不带转换器的路径,更新 [英] WPF Binding without path with converter, update

查看:94
本文介绍了WPF绑定,不带转换器的路径,更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用转换器没有路径绑定。这是因为转换器将使用对象的许多属性来构建工具提示的文本。但是,当一个属性更改时(实现了INotifyPropertyChanged并提高了OnPropertyChanged),这种没有路径的绑定不会得到更新。我猜是因为它不绑定到特定的属性。

I have a binding without path using a converter. This is because the converter will use many properties of the object to build the text for the tooltip. But when one property changes (INotifyPropertyChanged is implemented and OnPropertyChanged raised), this binding without path does not get updated. I guess because it does not bind to a specific property.

如何知道它必须更新?

我尝试更具体一点:

bar对象具有开始属性。当我更改此设置时,栏会及时移动,因为绑定直接进入Start属性。因此,通知适用于单个属性。但是工具提示绑定是 {Binding Converter = {StaticResource TooltipConverter}} ,并且不绑定到特定属性。当开始更改时,条形被移动,但工具提示不会更新,因为不会再次调用工具提示转换器。

The bar object has a 'Start' property. When I change this the bar moves in time because the binding gets directly to the Start property. So the notification works for single properties. But the tooltip binding is {Binding Converter={StaticResource TooltipConverter}} and does not bind to a specific property. When 'Start' changes, the bar is moved but the tooltip does not update because the tooltipconverter is not called again.

条形图是<$ c $中的一个对象c> ObservableCollection< Bar> 。条形图应该告诉集合或视图模型吗?通常与它没有任何关系。

The bar is one object in an ObservableCollection<Bar>. Should the bar tell the collection or the view model? Normally is would not have any relationship to it.

推荐答案

一种可能的解决方法是:

One possible workaround is:

为对象提供 ItSelf 属性(或其他名称),例如:

Give your object a ItSelf property (or other name) like:

public Object ItSelf
{
    get { return this; }
}

代替绑定

{Binding Converter={StaticResource TooltipConverter}}

使用

{Binding ItSelf, Converter={StaticResource TooltipConverter}}

然后为每个属性的 ItSelf筹集 OnPropertyChanged 。因此,只要绑定中使用了整个对象,您就可以发出整个对象的更新信号。

Then you raise OnPropertyChanged for ''ItSelf'' for every property. So you can signal an update for the whole object whenever it was used in a binding.

public DateTime Start
{
    get { return this.start; }
    set { this.start = value; OnPropertyChanged("Start"); OnPropertyChanged("ItSelf");
}

我的工作速度更快,但想测试一下 AttachedBehavior 就像@AnatoliiG所说的那样,所以我以后会接受答案。

I got this to work a bit faster but would like to test out AttachedBehavior for this like stated by @AnatoliiG so I will accept an answer later.

这篇关于WPF绑定,不带转换器的路径,更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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