DCSIMG
Silverlight Quick Tip: How to force re-binding - Alex Golesh's Blog About Silverlight Development

Silverlight Quick Tip: How to force re-binding

Today I’ll show how to do “dirty trick” to force re-binding and as a result UI value updating.

First of all – the reasonable question – “Why force rebinding? Why not use INotifyPropertyChanged mechanism?”

This was my questions also, but the person who asked the question had his reasons: he is using resources (Resx) to localize the application and bind the UI to those resources. Also UI gives user an ability to change the language (and code behind  does it by changing CurrentThread.CurrentUICulture). In this case what should be the notification?

The request was to keep binding definition at XAML and at the right time use those definitions to force the re-binding.

My approach was to get the BindingExpression from XAML element, and bind this XAML element to the same binding once again – this forces the binding

Sample code:

BindingExpression bindExp = txtSTR1.GetBindingExpression(TextBlock.TextProperty);
Binding bind = bindExp.ParentBinding;
txtSTR1.SetBinding(TextBlock.TextProperty, bind);
And this is actually works
image 
image 
Live demo here
Sources here
 
Enjoy,
Alex
Published Tuesday, July 14, 2009 12:21 PM by Alex Golesh

Comments

# Silverlight Quick Tip: RESX – Image Resources

This post was actually born in the middle of previous post :) I had to fin the way to use images as a

Tuesday, July 14, 2009 2:08 PM by DevCorner

# re: Silverlight Quick Tip: How to force re-binding

Can't you just do this

txtSTR1.GetBindingExpression(TextBlock.TextProperty).UpdateSource();

Wednesday, July 15, 2009 4:50 PM by dsoltesz

# re: Silverlight Quick Tip: How to force re-binding

dsoltesz:

No, because UpdateSource() updates Source (which in my case is ResX Resource image) from Target (which in my case is Image element on screen). Moreoever it works inly in TwoWay binding.

Regards,

Alex

Wednesday, July 15, 2009 5:10 PM by Alex Golesh

# re: Silverlight Quick Tip: How to force re-binding

Hi, I was facing the same problem in these days and I came up with a easier solution: I just added a "partial" modifier to the generated resource class and I have implemented the INotifyPropertyChanged in the custom partial resources class.

For example:

public partial class Res : INotifyPropertyChanged {

       public event PropertyChangedEventHandler PropertyChanged;

       protected void OnPropertyChanged(PropertyChangedEventArgs e)

       {

           PropertyChangedEventHandler handlers = this.PropertyChanged;

           if (handlers != null) handlers(this, e);

       }

       protected void RaisePropertyChanged(string propertyName)

       {

           this.OnPropertyChanged(new PropertyChangedEventArgs(propertyName));

       }

       protected void RaisePropertyChangedForAllProperties()

       {

           foreach (PropertyInfo pi in this.GetType().GetProperties(BindingFlags.Static | BindingFlags.Public)) this.RaisePropertyChanged(pi.Name);

       }

       public void Refresh()

       {

           this.RaisePropertyChangedForAllProperties();

       }

}

Thursday, July 23, 2009 5:05 PM by Aquilax

# re: Silverlight Quick Tip: How to force re-binding

I did it completely different. I too wanted to provide both Hebrew and English UI support (see link), I had all bindings attached to local UserControl/Page resources that were attached by code to an resx. They were updated by iterating over resources.

This way there is no need to know what controls(names) needs rebinding from code-Behind.

Tuesday, July 28, 2009 1:30 PM by Netanel KL

# re: Silverlight Quick Tip: How to force re-binding

Netanel KL:

"iterating over resources" IMHO is not the best way to do it, especially if your page has hundreds of controls :)

Regards,

Alex

Tuesday, July 28, 2009 2:36 PM by Alex Golesh

# re: Silverlight Quick Tip: How to force re-binding

Natanel KL:

And why you are not using Bidi controls (blogs.microsoft.co.il/.../silverlight-3-hebrew-and-arabic-support.aspx)??? It is better, than writing your hebrew characters reversed ;)

Tuesday, July 28, 2009 2:45 PM by Alex Golesh

# re: Silverlight Quick Tip: How to force re-binding

Hi Alex,

Regarding the first response, what's the alternative, suppose you have 100 'labels' are you gonna write three hundred lines from codebehind or three lines. Notice I'm iterating over page resources, in my case most page resources are the localization keys.

BTW I took the idea from:

wpf-e.spaces.live.com/.../cns!2B248D261D0E0035!203.entry

Regarding your suggestion to use the silverlight rtl, I had so many problems with it: Not all controls were implemented properly - even the Textbox, I couldn't work on Blend because the RTL library kept crashing it, and the most important problem - the wrapping (at least the last time I saw it) was ridiculous. I used a much better and very small-sized solution (only for textblocks):

blogs.microsoft.co.il/.../small-silverlight-library-with-rtl-hebrew-support-alpha-ver.aspx

I implemented a fix to the normal TextBox to work like a BiDi one. (Even posted the fix on the silverlight-RTL Issue-Tracker).

BTW how did you notice I wasn't using the Silverlight-RTL ?? :-)

Tuesday, July 28, 2009 9:43 PM by Netanel KL

# re: Silverlight Quick Tip: How to force re-binding

Netanel KL:

"BTW how did you notice I wasn't using the Silverlight-RTL ?? :-)" ==> I know my stuff ;)

Alex

Wednesday, July 29, 2009 8:17 AM by Alex Golesh

# re: Silverlight Quick Tip: How to force re-binding

Great site...keep up the good work.

Tuesday, September 01, 2009 11:47 PM by Bill Bartmann

# re: Silverlight Quick Tip: How to force re-binding

Cool site, love the info.

Thursday, September 03, 2009 6:31 PM by Bill Bartmann

# re: Silverlight Quick Tip: How to force re-binding

Excellent site, keep up the good work

Saturday, September 05, 2009 3:52 AM by Bill Bartmann

# re: Silverlight Quick Tip: How to force re-binding

This site rocks!

Monday, September 07, 2009 8:08 AM by Bill Bartmann

# re: Silverlight Quick Tip: How to force re-binding

Great site...keep up the good work.

Wednesday, September 09, 2009 6:50 PM by Bill Bartmann

# re: Silverlight Quick Tip: How to force re-binding

Excellent site, keep up the good work

Friday, September 11, 2009 7:36 AM by Bill Bartmann

# re: Silverlight Quick Tip: How to force re-binding

This site rocks!

Monday, September 14, 2009 5:08 AM by FAst Credit Repair

# re: Silverlight Quick Tip: How to force re-binding

There is a very useful article on the internet

"Complex Data Binding with the Accordion Control"

at aspalliance.com/1674_complex_data_binding_with_the_accordion_control.

I wonder if it is possible to make that sort of binding

in Silverlight3 Accordion control using .NET RIA Services.

Wednesday, October 14, 2009 1:39 AM by Van2010

# re: Silverlight Quick Tip: How to force re-binding

I like that you

think. Thank you for share very

Monday, May 03, 2010 7:05 PM by Best Registry Cleaner

# re: Silverlight Quick Tip: How to force re-binding

Thanks for posting! I really enjoyed the report. I've already bookmark

this article.

Tuesday, May 04, 2010 10:47 AM by tiffany jewellery

# re: Silverlight Quick Tip: How to force re-binding

One again, your articles is very good.thank you!very much.

Friday, May 07, 2010 10:48 AM by mbt shoes

# re: Silverlight Quick Tip: How to force re-binding

Truly cool article you have here. I'd like to read a bit more concerning such topic. Thnx for sharing such info.

Tuesday, May 11, 2010 12:28 AM by Hannah GFKStricker

# re: Silverlight Quick Tip: How to force re-binding

Really cool blog as for me. It'd be really cool to read more concerning such theme. Thnx for sharing such info.

Tuesday, May 11, 2010 9:11 PM by Ella LiveToBike

# re: Silverlight Quick Tip: How to force re-binding

Silverlight quick tip how to force re binding.. Amazing :)

Tuesday, April 19, 2011 8:03 PM by beta.blogs.microsoft.co.il

# re: Silverlight Quick Tip: How to force re-binding

Silverlight quick tip how to force re binding.. Nice :)

Friday, April 22, 2011 11:55 AM by beta.blogs.microsoft.co.il

# re: Silverlight Quick Tip: How to force re-binding

Silverlight quick tip how to force re binding.. Nifty :)

Friday, May 06, 2011 11:43 AM by beta.blogs.microsoft.co.il

# re: Silverlight Quick Tip: How to force re-binding

It is so nice!Worth to read!

Saturday, July 09, 2011 12:15 PM by Red Bottom Shoes

# re: Silverlight Quick Tip: How to force re-binding

The article is valuable,it is worth reading!

Saturday, July 09, 2011 12:16 PM by Juicy Couture Sale

# re: Silverlight Quick Tip: How to force re-binding

junk,,need to write hell of code for larger number of controls.

Monday, December 12, 2011 9:41 AM by khan

# re: Silverlight Quick Tip: How to force re-binding

Your content is very useful. Thank you so much for providing plenty of useful content. I have bookmarked your site and will be without doubt coming back. Once again, I appreciate all your work and also providing a lot vital tricks for your readers.

Tuesday, February 28, 2012 12:54 PM by Joshone

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above: