1
votes
 <TextBox Name="CustomerName" Height="30" Margin="5"
  Text="{Binding Source={StaticResource MyCustomerData},
   Path=CustomerName, Mode=TwoWay}" Grid.Row="0" Grid.Column="1" />

Above is my xaml snippet

MuCustomerData is my class which implements INotifyPropertyChanged interface and it has a property CustomerName which shall notify about the changes using PropertyChanged event to the View (UI)

Here I bind my class(MyCustomerData) as StaticResource and binding mode as TwoWay. Will this actually work two way? or I should use my class as Dynamic Resource to make the binding work two way? Since only once a StaticResource would get loaded and any further changes will not be taken when it is staticresource .... your thoughts?? I just read this http://www.codeproject.com/Articles/393086/WPF-StaticResource-vs-DynamicResource ..Pls do take a look on this article ..

2
Of course when you make the mode of binding as Two way it works as two way , what made you think it may not work ?srsyogesh
@srsyogesh well I think when the resource is a dynamicresource and mode is two way then the binding works two way whereas if the resource is a staticResource and binding is two way it does not.. I aint sure though ..thats y posted the ques!Arvind897
Try to make your Question title a little shorter and put the actual question in the content portion. Also, I am not seeing the XAML code you supposedly posted. Please revise.Scott Solmer
@Okuma.Scott revised thanks for ur suggestion :)Arvind897

2 Answers

4
votes

StaticResource and DynamicResource refer to WPF's Resources, not Bindings.

StaticResource evaluates once (usually when the XAML is parsed), and is never evaluated again because WPF assumes the resource is static and won't ever change.

DynamicResource means the resource is dynamic, so evaluate it whenever the value is needed.

In your case, the Source property of your binding will be evaluated once, and never again since it is set to a StaticResource. If you change the MyCustomerData object to a new object, the binding won't evaluate itself again to reflect the change.

But the actual property, CustomerName, will get updated as needed because it is bound using a TwoWay binding.

To look at it another way, you're creating a binding that says something like

var b = new Binding();
b.Source = MyCustomerData;
b.Path = "Name";
b.Mode = TwoWay;

When you set b.Source using a StaticResource, then think of the binding evaluating using b.Source.Name. But if you were to use a DynamicResource, think of it as evaluating with MyCustomerData.Name, and so it would use the current version of MyCustomerData.

So you could set MyCustomerData = new MyCustomerData();` after the binding has been evaluated once, and a dynamic resource would evaluate that correctly, while a static resource would not.

0
votes

StaticResource or DynamicResource have nothing to do with Binding.

I will try to explain this as simple as possible for you.

StaticResource means that you know in which resource dictionary the object with specific key is being hold. Thats it. Nothing else. You know its either in StackPanel.Resources or maybe Window.Resources.

DynamicResource is gonna be used when you add/load somewhere in your VisualTree an object with a key into resources dictionary. It's position is unknown at app's startup and so it needs to be loaded dynamically.

Remember no matter what kind of language you use for programming whenever you see dynamics being mentioned it means something is happening at runtime and not right from beginning.

Best example would be dynamically generated objects in C#. Those objects allow you to define a property at runtime. When the app is started the object is pretty empty.

Now back to Binding. In your case you tell the Binding where the Source is and from that point on Binding will work without giving a s**t how is Source defined.