Search code examples
silverlightdata-bindingpropertychanged

Silverlight: Setting property directly removes data binding?


I am modifying a Silverlight project which heavily uses the code behind for setting properties and such. This was created a few years back and was more or less ported over from a WinForms project.

The problem I noticed is that when I set a data binding for a property, Visibility in this case, if that property is set directly in the code behind then the data binding is removed. I even set break points on the PropertyChanged event to verify that this is the case.

I have done quite a bit of WPF development and never noticed this. Is this normal and I missed it or is something not quite right?


Solution

  • Seems quite normal to me.

    The concept behind the binding is that the value on the target property is bound to the property on the source object. Whilst bound the target property will always reflect the value of the source property. As soon as you assign your own value to the target property the target property no longer reflects the value from the source, you've broken the binding between the two. Hence setting your own value removes the binding.

    However if you set the Mode of the binding to TwoWay then Silverlight simply assigns the new value to the property on the source object and maintains the binding since the target property still reflects the value of the source property.