views:

4422

answers:

3

I'm missing the boat on something here, kids. This keeps rearing its head and I don't know what's going on with it, so I hope my homeys here can help.

When working in Silverlight, when I create bindings in my c# code, they never hold up when the application is running. The declarative bindings from my xaml seem ok, but I'm doing something wrong when I create my bindings in C#. I'm hoping that there is something blindingly obvious I'm missing. Here's a typical binding that gets crushed:

TextBlock tb = new TextBlock();
Binding b = new Binding("FontSize");
b.Source = this;
tb.SetBinding(TextBlock.FontSizeProperty, b);
+2  A: 

I've just tried the exact code you just posted and it worked fine, with some changes. I believe the problem is the element you are using for the SetBinding call is not the textblock you want to bind. It should be:

TextBlock tb = new TextBlock();
Binding b = new Binding("FontSize");
b.Source = this;
tb.SetBinding(TextBlock.FontSizeProperty, b);

Make sure you also have a FontSize public property of type double on "this". If "this" is a user control, I would recommend renaming the property so you don't hide the inherited member.

You're right, that was actually just a copy/paste artifact. In this case, `this` is a custom control and I'm referring to its inherited FontSize property. I see this happening all over. I'm afraid it's something I'm doing that actually removes the binding, but I can't identify it.
MojoFilter
A: 

I don't have rights to change a property name because property name should be intuitive. And my company won’t allow me to change the name of a property. It has to be FontSize only. Is there any way to achieve this? Please help.

Is it the problem with Silverlight that programmatic binding is not working? I have checked with WPF. Same code works well from XAML and managed code both.

Regards, Somnath

Somnath
A: 

It looks like as of Silverlight 3.1, at least, this is no longer an issue. I can't reproduce it, at any rate.

MojoFilter
I can reproduce something like ithttp://stackoverflow.com/questions/3217740/silverlight-4-setbinding-not-working
cmaduro