Monday, June 16, 2008

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.

No comments: