UserControl binding to own property bug in Sliverlight 2.0 beta 2
Today morning, I come to my office. Alex, sitting there, was very tired. His eyes were red. I asked him: “What’s going on?” and he responsed: “I think, I’m sick. Something is broken in Silverlight”. “What’s the problem?” – I asked. And he explained me, that he want to create UserControl with some properties. Let’s say something like this:
<UserControl x:Class="SilverlightControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300">
<Grid x:Name="LayoutRoot" Background="White">
<TextBlock Text="Hello, World" Name="tb" FontSize="{Binding MySize}"/>
</Grid>
</UserControl>
Where MySize is internal dependency property of the SilverlightControl1. Now you’re going to your application and consuming your new control.
<l:SilverlightControl1 x:Name="kuku" MySize="40"/>
This does not work. Maybe the problem is DataContext? Let’s set DataContext of UserControl to itself
public SilverlightControl1()
{
InitializeComponent();
this.DataContext = this;
}
Ups… “An unhandled exception of type ‘System.StackOverflowException’ occurred in System.Windows.dll”. Well, why binding does not happens?
Maybe the problem is binding? Let’s try to explicitely bind to it property.
Binding b = new Binding("MySize");
b.Source = this;
tb.SetBinding(TextBlock.FontSizeProperty, b);
Nothing happens. Where to dig? Maybe notification changes not happen? Let’s add INotifyPropertyChanged notification
public double MySize
{
get { return (double)GetValue(MySizeProperty); }
set { SetValue(MySizeProperty, value); PropertyChanged(this,new PropertyChangedEventArgs("MySize"));}
}
Now it works, but what will happen if we’ll remove explicit binding? Nothing this does not work. What’s the problem?
The problem is bug in Silverlight 2.0 beta 2. To workaround it, you can either use explicit binding inside the usercontrol, or (like me) create external data class and bind to this class.
public class MyData : INotifyPropertyChanged
{
public double MySize
{
get { return m_MySize; }
set
{
m_MySize = value;
OnPropertyChanged("MySize");
}
}double m_MySize = default(double);
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string name)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(name));
}#endregion
}
Create DataContext
MyData d;
public SilverlightControl1()
{
d = new MyData();
InitializeComponent();
this.DataContext = d;
And then, extern the property to the control
public double MySize
{
get { return d.MySize; }
set { d.MySize = value; }
}
We done. Hope this nasty bug will be fixed in RTM version of Silverlight. Have a good day and be nice people.
Have a nice day to Alex to. Do not try to understand what’s problem with your code, when working with beta version of any framework. First check alternatives.
You may also be interested with:
- INotifyPropertyChanged auto wiring or how to get rid of redundant code
- Real singleton approach in WPF application
June 15th, 2008 · Comments (4)
4 Responses to “UserControl binding to own property bug in Sliverlight 2.0 beta 2”
Leave a Reply
Discover other tags
My tools
- .NET Framework Detector
- Duplicate images finder
- Exchange Security Policy for Windows Mobile Devices Fix
- Gas Price Windows Vista SideBar gadget
- Israel Traffic Information Windows Vista SideBar gadget
- Localization fix for SAP ES Explorer for Visual Studio
- LocTester
- RTL and LTR in Windows Live Writer
- Silverlight controls library
- Snipping tool integration plugin for WLW
- USB FM receiver library
- Vista Battery Saver
- WebCam control for WPF
- Windows Live SkyDrive attachment for Windows Live Writer
- Wireless Migrator
- WPF Virtual Keyboard




January 1st, 2009 at 12:51 am
Good day,
I have the same problem. I have a user control with a textblock and textbox (simple). Binding used to work but since Beta2, same error as your thread.
I tried for many hours to make your example work with no luck; it is not clear how the binding actually is done from the main program on to the MyData class.
My main problem though is that the property in the control (MySize) is never called when the data changes. I managed to get the TextChanged event called but the property MySize is never executed so the value doesn’t get updated.
Hope you have an idea!
cheers.
January 1st, 2009 at 12:51 am
Mino has a SL2B2 video player drop-in, Emil Stoychev on Custom Controls, Tamir Khason on a binding bug
March 26th, 2009 at 3:43 pm
You will be better off using the RelativeSource property. I stumbled on to this page when I faced the same problem. Here is how i solved it
This saves you from creating a new class! i just figured it out and it works for me.
March 26th, 2009 at 3:43 pm
Text=”{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type l:SilverLightControl1}},Path=MySize}”>