回答済み Binding to an object in a resource dictionary

  • Monday, January 05, 2009 12:02 AM
     
     
    Hi!
    I'm new to WPF, so excuse me if this question seems to be really stupid.
    It seems that if you want to bind something to something else in a way that the target is automatically updated when the source change, the source has to implement INotifyPropertyChange interface. Right?

    Well now, in my application I put a value of type "double" into the resource dictionary of the Application object  and i wrote a simple control:

    <Path 
    Height="9" 
            Margin="0,0,3,0" 
            VerticalAlignment="Stretch"
            Opacity="{DynamicResource MyDouble}">

    I can see that, if i change (in procedural code) the value of that double:

    Application.Current.Resource["MyDouble"] = ...

    the opacity of the Path object changes correctly.

    Anyway it seems that ResourceDictionary doesn't implement INotifyPropertyChange or INotifyCollectionChange.
    I'm wondering what would happen if i put into the resource dictionary an object with a property, bind to this property, and change it. Does the change of this property reflect on the target?

    Thank you!

All Replies

  • Monday, January 05, 2009 8:54 AM
     
     
    I prefer using denpendency properties than implementing the INotify....
    Just use a dependency property to hold your double if it's OK to do so.
    Daniel
  • Tuesday, January 06, 2009 12:11 PM
     
     Answered

    Hello Gabriele,

      In addition to Daniel's,

      If your source object implements a proper notification mechanism target updates happen automatically. If for any reason your source object does not provide the proper property changed notifications, you have the option to use the UpdateTarget method to update the target property explicitly. 

      More in  Binding Sources Overview


     Anyway it seems that ResourceDictionary doesn't implement INotifyPropertyChange or INotifyCollectionChange.

    I'm wondering what would happen if i put into the resource dictionary an object with a property, bind to this property, and change it. Does the change of this property reflect on the target?

      No. As I know we cannot do it. We have to implement the INotifyPropertyChange interface on the object class.

    Thanks.


    Please mark the replies as answers if they help and unmark them if they provide no help
    • Edited by Hua Chen Tuesday, January 06, 2009 12:26 PM edit.
    • Marked As Answer by Hua Chen Friday, January 09, 2009 5:51 AM
    •