开发者

how do i force the converter to execute when the property is changed?

This is my image in XAML:

<Image Margin="0"
       Stretch="UniformToFill"
       Source="{Binding '', Converter={StaticResource byteArrToBitmap}}">
   <ToolTipService.ToolTip>
        <Border  BorderBrush="#FF3D3D3D"  Background="#FFFFE1E1">
            <TextBlock Text="{Binding PhotoDescription, TargetNullValue=No description}"
                       Width="170"
                       Height="Auto"
                       FontFamily="Georgia"
                       TextWrapping="Wrap"
                       Foreground="#FF373737"/>
        </Border>
   </ToolTipService.ToolTip>                        
</Image>

This Image is inside DataTemplate of listbox. As you can see I have source set to {Binding ''} which means it is bound to datacontext and not to the actual property that I want to bind. This is essential because I have some logic being performed based on which I am returning an image.

I am downloading the images on the fly from webservices and it returns a byte[]. I have INotifyPropertyChanged implemented in the class. However, as I have binding setup to the DataContext, the converter does not reexecut开发者_StackOverflowe itself when the byte[] is downloaded in asynchronous manner.


It's a verty bad idia to bind something to the DataContext it self.. right now Silverligth 4 do not implement INotifyPropertyChanged for DataContext, so you have two options:

1) wait for Silverligt 5:

Silverlight 5–Features list

The DataContextChanged event is being introduced. Markup extensions allow code to be run at XAML parse time for both properties and event handlers, enabling cutting-edge MVVM support.

2) create some object that implement INotifyPropertyChanged, create some property, and bind to that property...


I believe you just want

{Binding Converter={StaticResource byteArrToBitmap}}

Not

{Binding '', Converter={StaticResource byteArrToBitmap}}

As not specifying any property path will bind to the DataContext. I have no idea what {Binding ''} does, but it's not standard practice. I'm surprised it doesn't throw an exception, actually.

That said, the way I would handle this is to have a wrapper object, which has a property representing the byte array - that way you can raise INotifyPropertyChanged events in a more straightforward way. I believe there's a way to invalidate the whole object, but I don't recall what it is.


I am assuming that your data context is the byte[] that you want to convert. So you have to ensure that the PropertyChanged event is raised whenever the asynchronous download is complete. Also, ensure that the event is raised on the Main Thread and not on a Worker or Background thread.


If you change your binding to be bound to some property, even if its unnecessary in the scope of your application you can indirectly cause the Converter to reevaluate when that property changes. That property needs to exist on an object that implements INotifyPropertyChanged.

I can give source code if needed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜