WPF更新绑定直接绑定到DataContext w /转换器 [英] WPF Update Binding when Bound directly to DataContext w/ Converter

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

问题描述

通常情况下,当您想要一个数据绑定控件进行更新时,您可以使用PropertyChanged事件向接口发出数据在幕后更改的信息。



例如,你可以有一个textblock绑定到一个属性DisplayText的数据文本



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



从这里,如果DataContext使用PropertyNameDisplayText引发PropertyChanged事件,则此文本块的文本应该更新(假设您没有更改绑定的模式)。



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



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



正如你所看到的,我使用没有路径的{Binding}直接绑定到数据报文,我使用ImageConverter选择我想要的图像。但是现在我没有办法(我知道)来说明绑定更新。我尝试用。提高propertychanged事件。作为属性名称,没有工作。



这是可能吗?我必须将转换逻辑包装成绑定可以附加到的属性,还是有方法告诉绑定刷新(没有明确刷新绑定)?



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

解决方案

这里的解决方法是将一个属性添加到我的对象(用作数据报文)称为自我,简单地返回



public Object Self {get {return this; }}



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



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



然后当我调用



PropertyChanged(this,new PropertyChangedEventArgs(Self))



它的作用就像一个魅力。



感谢所有。


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

解决方案

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.

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

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