Silverlight Quick Tip: How to force re-binding

July 14, 2009

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

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

28 comments

  1. dsolteszJuly 15, 2009 ב 16:50

    Can’t you just do this

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

    Reply
  2. Alex GoleshJuly 15, 2009 ב 17:10

    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

    Reply
  3. AquilaxJuly 23, 2009 ב 17:05

    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();
    }
    }

    Reply
  4. Netanel KLJuly 28, 2009 ב 13:30

    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.

    Reply
  5. Alex GoleshJuly 28, 2009 ב 14:36

    Netanel KL:

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

    Regards,

    Alex

    Reply
  6. Alex GoleshJuly 28, 2009 ב 14:45

    Natanel KL:
    And why you are not using Bidi controls (http://blogs.microsoft.co.il/blogs/alex_golesh/archive/2009/07/10/silverlight-3-hebrew-and-arabic-support.aspx)??? It is better, than writing your hebrew characters reversed ;)

    Reply
  7. Netanel KLJuly 28, 2009 ב 21:43

    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:
    http://wpf-e.spaces.live.com/blog/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):

    http://blogs.microsoft.co.il/blogs/maxim/archive/2008/12/14/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 ?? :-)

    Reply
  8. Alex GoleshJuly 29, 2009 ב 8:17

    Netanel KL:
    “BTW how did you notice I wasn’t using the Silverlight-RTL ?? :-) ” ==> I know my stuff ;)

    Alex

    Reply
  9. Bill BartmannSeptember 1, 2009 ב 23:47

    Great site…keep up the good work.

    Reply
  10. Bill BartmannSeptember 5, 2009 ב 3:52

    Excellent site, keep up the good work

    Reply
  11. Bill BartmannSeptember 9, 2009 ב 18:50

    Great site…keep up the good work.

    Reply
  12. Bill BartmannSeptember 11, 2009 ב 7:36

    Excellent site, keep up the good work

    Reply
  13. Van2010October 14, 2009 ב 1:39

    There is a very useful article on the internet
    “Complex Data Binding with the Accordion Control”
    at http://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.

    Reply
  14. Best Registry CleanerMay 3, 2010 ב 19:05

    I like that you

    think. Thank you for share very

    Reply
  15. tiffany jewelleryMay 4, 2010 ב 10:47

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

    this article.

    Reply
  16. mbt shoesMay 7, 2010 ב 10:48

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

    Reply
  17. Hannah GFKStrickerMay 11, 2010 ב 0:28

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

    Reply
  18. Ella LiveToBikeMay 11, 2010 ב 21:11

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

    Reply
  19. beta.blogs.microsoft.co.ilApril 19, 2011 ב 20:03

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

    Reply
  20. beta.blogs.microsoft.co.ilApril 22, 2011 ב 11:55

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

    Reply
  21. beta.blogs.microsoft.co.ilMay 6, 2011 ב 11:43

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

    Reply
  22. Red Bottom ShoesJuly 9, 2011 ב 12:15

    It is so nice!Worth to read!

    Reply
  23. Juicy Couture SaleJuly 9, 2011 ב 12:16

    The article is valuable,it is worth reading!

    Reply
  24. khanDecember 12, 2011 ב 9:41

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

    Reply
  25. JoshoneFebruary 28, 2012 ב 12:54

    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.

    Reply