Silverlight 2.0 - Bug with Data Binding?

Location: BlogsRainer's BlogSilverlight   
Posted by: Rainer Stropek2008-03-16 13:02:33Z

The good news is that Silverlight 2.0 supports Data Binding. However, when I wanted to use it it did not work the way I expected it.

The Problem

Check out this Silverlight 2.0 XAML file:

    xmlns="http://schemas.microsoft.com/client/2007"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="400" Height="300">
   
       
       
   

Here is the corresponding code behind:

using System.Windows;
using System.Windows.Controls;
using System.ComponentModel;

namespace SilverlightDataBinding
{
    public partial class Page : UserControl
    {
        public Page()
        {
            DataContext = this;
            SetValue(HelloTextProperty, "Silverlight is cool");

            InitializeComponent();

            ChangeHelloText.Click += new RoutedEventHandler(ChangeHelloText_Click);
        }

        void ChangeHelloText_Click(object sender, RoutedEventArgs e)
        {
            SetValue(HelloTextProperty, HelloText + " cool");
        }

        public string HelloText
        {
            get { return (string)GetValue(HelloTextProperty); }
            set { SetValue(HelloTextProperty, value); }
        }
        public static readonly DependencyProperty HelloTextProperty =
            DependencyProperty.Register("HelloText", typeof(string), typeof(Page), null);
    }
}

If I run this application I can click on the button as often as I want. The content of the TextBlock never changes. However, in Visual Studio I can see that the value of the dependency property HelloText has changed! Seems to be a problem with refreshing the screen appropriately.

A Solution With CLR Objects

I wanted to know if the refresh problem also occurs with CLR objects that implement INotifyPropertyChanged. Therefore I changed the app shown above a little bit and I added a class.

Here is the XAML file after the changes:

    xmlns="http://schemas.microsoft.com/client/2007"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="400" Height="300">
   
       
       
       
   

Here is the new class:

using System.ComponentModel;

namespace SilverlightDataBinding
{
    public class TextContainer : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private string helloText;
        public string HelloText
        {
            get
            {
                return helloText;
            }
            set
            {
                if (helloText != value)
                {
                    helloText = value;
                    if (PropertyChanged != null)
                        PropertyChanged(this, new PropertyChangedEventArgs("HelloText"));
                }
            }
        }
    }
}

Last but not least the code behind file:

using System.Windows;
using System.Windows.Controls;
using System.ComponentModel;

namespace SilverlightDataBinding
{
    public partial class Page : UserControl
    {
        private TextContainer textContainer;

        public Page()
        {
            DataContext = this;
            SetValue(HelloTextProperty, "Silverlight is cool");
            textContainer = new TextContainer();
            textContainer.HelloText = "Silverlight is cool";

            InitializeComponent();

            ChangeHelloText.Click += new RoutedEventHandler(ChangeHelloText_Click);
            TextBlock_CLR.DataContext = textContainer;
        }

        void ChangeHelloText_Click(object sender, RoutedEventArgs e)
        {
            SetValue(HelloTextProperty, HelloText + " cool");
            textContainer.HelloText += " cool";
        }

        public string HelloText
        {
            get { return (string)GetValue(HelloTextProperty); }
            set { SetValue(HelloTextProperty, value); }
        }
        public static readonly DependencyProperty HelloTextProperty =
            DependencyProperty.Register("HelloText", typeof(string), typeof(Page), null);
    }
}

Believe it or not - the CLR class with INotifyPropertyChanged works!

Getting Dependency Properties to Work

Finally I found out that refreshing the screen works correctly with Dependency Properties if the object containing them also implements INotifyPropertyChanged:

using System.Windows;
using System.Windows.Controls;
using System.ComponentModel;

namespace SilverlightDataBinding
{
    public partial class Page : UserControl, INotifyPropertyChanged
    {
        private TextContainer textContainer;

        public event PropertyChangedEventHandler PropertyChanged;

        public Page()
        {
            DataContext = this;
            SetValue(HelloTextProperty, "Silverlight is cool");
            textContainer = new TextContainer();
            textContainer.HelloText = "Silverlight is cool";

            InitializeComponent();

            ChangeHelloText.Click += new RoutedEventHandler(ChangeHelloText_Click);
            TextBlock_CLR.DataContext = textContainer;
        }

        void ChangeHelloText_Click(object sender, RoutedEventArgs e)
        {
            SetValue(HelloTextProperty, HelloText + " cool");
            textContainer.HelloText += " cool";
        }

        public string HelloText
        {
            get { return (string)GetValue(HelloTextProperty); }
            set { SetValue(HelloTextProperty, value); }
        }
        public static readonly DependencyProperty HelloTextProperty =
            DependencyProperty.Register("HelloText", typeof(string), typeof(Page),
            HelloTextChangedCallback);
        public static void HelloTextChangedCallback( DependencyObject d, DependencyPropertyChangedEventArgs e )
        {
            var p = (Page)d;
            if (p.PropertyChanged != null)
                p.PropertyChanged(p, new PropertyChangedEventArgs("HelloText"));
        }
    }
}

I assume that this is a bug in Silverlight 2.0 Beta. Otherwise Silverlight would behave completely different than WPF.

Permalink | Trackback

Comments (11)  Add Comment
Re: Silverlight 2.0 - Bug with Data Binding?  By Andrew on 2008-04-02 14:16:53Z
Thanks for this explanation! I have the same issue.

Re: Silverlight 2.0 - Bug with Data Binding?  By KierenH on 2008-09-13 05:42:22Z
This is as designed. That's right, Dps in Siverlight behave different to DPs in Wpf. <br>Note: The core DPs support change notification, custom DPs will need a mechanism like above (INotifyPropertyCHanged) to raise the change externally.<br>Basically all a DP is good for now is for use in custom controls.<br>Use a DP when you want to show it in your control, so when you template it:<br><TextBlock Text="{TemplateBinding MyDP}" /><br>Also note, Silverlight does not support converters in templates.<br><br>Shall I keep going :( ...

Re: Silverlight 2.0 - Bug with Data Binding?  By KierenH on 2008-09-13 08:15:34Z
This is as designed. That's right, Dps in Siverlight behave different to DPs in Wpf. <br>Note: The core DPs support change notification, custom DPs will need a mechanism like above (INotifyPropertyCHanged) to raise the change externally.<br>Basically all a DP is good for now is for use in custom controls.<br>Use a DP when you want to show it in your control, so when you template it:<br><TextBlock Text="{TemplateBinding MyDP}" /><br>Also note, Silverlight does not support converters in templates.<br><br>Shall I keep going :( ...

Re: Silverlight 2.0 - Bug with Data Binding?  By Shazia on 2008-12-17 13:41:55Z
Silverlight 2.0 - Bug with Data Binding is very useful and beautiful. This blog have good information.<br><br>Regard<br>http://www.mayfairstationers.co.uk

Re: Silverlight 2.0 - Bug with Data Binding?  By pakistanjobs.pk on 2009-05-12 08:10:46Z
Topic of Silverlight 2.0 - Bug with Data Binding? is very beautiful and interesting.<br><br>Regard<br>http://www.pakistanjobs.pk

Re: Silverlight 2.0 - Bug with Data Binding?  By D on 2010-04-08 10:09:33Z
<h1 style="font--size:12px"><br><A href="http://www.salelouboutin.com/christian-louboutin-pumps-c-78.html">christian louboutin</A>--<A href="http://www.buylouboutin.com">louboutin</A>--<A href="http://www.discount--christianlouboutin.com">christian louboutin</A>--<A href="http://www.discount--christianlouboutin.com">louboutin shoes</A>--<A href="http://www.discount--christianlouboutin.com">christian louboutin boots</A>--<A href="http://www.bestlouisvuitton.com">lv handbags</A>--<A href="http://www.sale--mbt.com">mbt shoes</A>--<A href="http://www.discount--christianlouboutin.com">christian louboutin</A>--<A href="http://www.sale--mbt.com/nike--shox--c--29.html">nike shoes</A>--<A href="http://www.sale--mbt.com/nike--shox--c--29.html">cheap nike shoes</A>--<A href="http://www.sale--mbt.com/nike--shox--c--29.html">discount nike shoes</A>--<A href="http://www.sale--mbt.com/nike--shox--c--29.html">nike shoes sale</A>--<A href="http://www.discount--christianlouboutin.com/jimmy--choo--shoes--c--47.html">Jimmy choo</A>--<A href="http://www.discount--christianlouboutin.com/jimmy--choo--shoes--c--47.html">Jimmy choo shoes</A>--<A href="http://www.discount--christianlouboutin.com/jimmy--choo--shoes--c--47.html">cheap Jimmy choo</A>--<A href="http://www.discount--christianlouboutin.com/jimmy--choo--shoes--c--47.html">discount Jimmy choo</A>--<A href="http://www.discount--christianlouboutin.com/jimmy--choo--shoes--c--47.html">Jimmy choo shoes sale</A>--<A href="http://www.sale--mbt.com/moncler--women--c--19.html">moncler women</A>--<A href="http://www.sale--mbt.com/moncler--women--c--19.html">cheap moncler women</A>--<A href="http://www.sale--mbt.com/moncler--women--c--19.html">discount moncler women</A>--<A href="http://www.sale--mbt.com/moncler--women--c--19.html">moncler women sale</A>--<A href="http://www.discount--christianlouboutin.com/manolo--blahnik--c--48.html">Manolo Blahnik</A>--<A href="http://www.discount--christianlouboutin.com/manolo--blahnik--c--48.html">Manolo Blahnik shoes</A>--<A href="http://www.discount--christianlouboutin.com/manolo--blahnik--c--48.html">cheap Manolo Blahnik</A>--<A href="http://www.discount--christianlouboutin.com/manolo--blahnik--c--48.html">discount Manolo Blahnik</A>--<A href="http://www.discount--christianlouboutin.com/manolo--blahnik--c--48.html">Manolo Blahnik sale</A>--<A href="http://www.sale--mbt.com/moncler--men--c--17.html">moncler men</A>--<A href="http://www.sale--mbt.com/moncler--men--c--17.html">cheap moncler men</A>--<A href="http://www.sale--mbt.com/moncler--men--c--17.html">discount moncler men</A>--<A href="http://www.sale--mbt.com/moncler--men--c--17.html">moncler men sale</A>--<A href="http://www.discount--christianlouboutin.com/balmain--shoes--c--50.html">Balmain Shoes</A>--<A href="http://www.discount--christianlouboutin.com/balmain--shoes--c--50.html">cheap Balmain Shoes</A>--<A href="http://www.discount--christianlouboutin.com/balmain--shoes--c--50.html">discount Balmain Shoes</A>--<A href="http://www.discount--christianlouboutin.com/balmain--shoes--c--50.html">Balmain Shoes sale</A>--<A href="http://www.sale--mbt.com/moncler--kids--c--18.html">moncler kids</A>--<A href="http://www.sale--mbt.com/moncler--kids--c--18.html">cheap moncler kids</A>--<A href="http://www.sale--mbt.com/moncler--kids--c--18.html">discount moncler kids</A>--<A href="http://www.sale--mbt.com/moncler--kids--c--18.html">moncler kids sale</A>--<A href="http://www.discount--christianlouboutin.com/yves--saint--laurent--c--49.html">Yves saint Laurent</A>--<A href="http://www.discount--christianlouboutin.com/yves--saint--laurent--c--49.html">Yves saint Laurent shoes</A>--<A href="http://www.discount--christianlouboutin.com/yves--saint--laurent--c--49.html">cheap Yves saint Laurent</A>--<A href="http://www.discount--christianlouboutin.com/yves--saint--laurent--c--49.html">discount Yves saint Laurent</A>--<A href="http://www.discount--christianlouboutin.com/yves--saint--laurent--c--49.html">Yves saint Laurent sale</A>--<A href="http://www.sale--mbt.com/">cheap mbt shoes</A>--<A href="http://www.sale--mbt.com/">discount mbt shoes</A>--<A href="http://www.sale--mbt.com/">mbt shoes sale</A>--<A href="http://www.discount--christianlouboutin.com/ed--hardy--c--46.html">Ed Hardy</A>--<A href="http://www.discount--christianlouboutin.com/ed--hardy--c--46.html">Ed Hardy shoes</A>--<A href="http://www.discount--christianlouboutin.com/ed--hardy--c--46.html">cheap Ed Hardy</A>--<A href="http://www.discount--christianlouboutin.com/ed--hardy--c--46.html">discount Ed Hardy</A>--<A href="http://www.discount--christianlouboutin.com/ed--hardy--c--46.html">Ed Hardy sale</A>--<A href="http://www.discount--christianlouboutin.com/ugg--boots--c--57.html">UGG boots</A>--<A href="http://www.discount--christianlouboutin.com/ugg--boots--c--57.html">cheap UGG boots</A>--<A href="http://www.discount--christianlouboutin.com/ugg--boots--c--57.html">discount UGG boots</A>--<A href="http://www.discount--christianlouboutin.com/ugg--boots--c--57.html">UGG boots sale</A>--<A href="http://www.discount--christianlouboutin.com/alexander--mcqueen--c--51.html">Alexander McQueen</A>--<A href="http://www.discount--christianlouboutin.com/alexander--mcqueen--c--51.html">Alexander McQueen shoes</A>--<A href="http://www.discount--christianlouboutin.com/alexander--mcqueen--c--51.html">cheap Alexander McQueen</A>--<A href="http://www.discount--christianlouboutin.com/alexander--mcqueen--c--51.html">discount Alexander McQueen</A>--<A href="http://www.discount--christianlouboutin.com/alexander--mcqueen--c--51.html">Alexander McQueen sale</A>--<A href="http://www.discount--christianlouboutin.com/chanel--shoes--c--52.html">Chanel Shoes</A>--<A href="http://www.discount--christianlouboutin.com/chanel--shoes--c--52.html">cheap Chanel Shoes</A>--<A href="http://www.discount--christianlouboutin.com/chanel--shoes--c--52.html">discount Chanel Shoes</A>--<A href="http://www.discount--christianlouboutin.com/chanel--shoes--c--52.html">Chanel Shoes sale</A>--<A href="http://www.discount--christianlouboutin.com/gucci--shoes--c--53.html">Gucci Shoes</A>--<A href="http://www.discount--christianlouboutin.com/gucci--shoes--c--53.html">cheap Gucci Shoes</A>--<A href="http://www.discount--christianlouboutin.com/gucci--shoes--c--53.html">discount Gucci Shoes</A>--<A href="http://www.discount--christianlouboutin.com/gucci--shoes--c--53.html">Gucci Shoes sale</A>--<A href="http://www.discount--christianlouboutin.com/tory--burch--shoes--c--54.html">Tory Burch Shoes</A>--<A href="http://www.discount--christianlouboutin.com/tory--burch--shoes--c--54.html">Tory Burch</A>--<A href="http://www.discount--christianlouboutin.com/tory--burch--shoes--c--54.html">cheap Tory Burch Shoes</A>--<A href="http://www.discount--christianlouboutin.com/tory--burch--shoes--c--54.html">discount Tory Burch Shoes</A>--<A href="http://www.discount--christianlouboutin.com/tory--burch--shoes--c--54.html">Tory Burch Shoes sale</A>--<A href="http://www.discount--christianlouboutin.com/lanvin--shoes--c--56.html">Lanvin Shoes</A>--<A href="http://www.discount--christianlouboutin.com/lanvin--shoes--c--56.html">cheap Lanvin Shoes</A>--<A href="http://www.discount--christianlouboutin.com/lanvin--shoes--c--56.html">Lanvin Shoes</A>--<A href="http://www.discount--christianlouboutin.com/lanvin--shoes--c--56.html">Lanvin Shoes sale</A>--<A href="http://www.discount--christianlouboutin.com/bottega--veneta--c--55.html">Bottega Veneta</A>--<A href="http://www.discount--christianlouboutin.com/bottega--veneta--c--55.html">Bottega Veneta shoes</A>--<A href="http://www.discount--christianlouboutin.com/bottega--veneta--c--55.html">cheap Bottega Veneta</A>--<A href="http://www.discount--christianlouboutin.com/bottega--veneta--c--55.html">discount Bottega Veneta</A>--<A href="http://www.discount--christianlouboutin.com/bottega--veneta--c--55.html">Bottega Veneta sale</A>--<br></h1>

Re: Silverlight 2.0 - Bug with Data Binding?  By ww on 2010-07-24 04:55:41Z
Harjutus, tervis. Seurasimme samal ajal. Seljas meie mbt zapatos oli lihtsalt jalgsi iga päev mängida rolli teostada, kui asjakohane, siis lisada mõned sörkimine, mängides nagu tugev liikumine võib olla tõhusam teha meie keha tugev. Mis on värske! Mbt zapatos teie abi selle toetuseks.

Re: Silverlight 2.0 - Bug with Data Binding?  By ww on 2010-07-24 04:56:01Z
Harjutus, tervis. Seurasimme samal ajal. Seljas meie<a href="http://www.mbt-zapatos-2010.com"><strong>mbt zapatos</strong></a>oli lihtsalt jalgsi iga päev mängida rolli teostada, kui asjakohane, siis lisada mõned sörkimine, mängides nagu tugev liikumine võib olla tõhusam teha meie keha tugev. Mis on värske!<a href="http://www.mbt-zapatos-2010.com"><strong>Mbt zapatos</strong></a>teie abi selle toetuseks.

Re: Silverlight 2.0 - Bug with Data Binding?  By wgsa on 2010-07-28 06:08:32Z
<a href="http://www.mbt-zapatos-2010.com"><strong>Mbt zapatos</strong></a>como una tormenta se extendió por Europa y el mundo.<a href="http://www.mbt-zapatos-2010.com/free-shipping-mbts-c-272.html"><strong>Mbt zapatos</strong></a>cómo tener tanto poder? Según la investigación,<a href="http://www.mbt-zapatos-2010.com/men-mbt-shoes-c-242.html"><strong>Mbt zapatos</strong></a>no sólo su propio estilo de diseño único, sino también porque hay magia buena forma física, así<a href="http://www.mbt-zapatos-2010.com/women-mbt-shoes-c-240.html"><strong>Mbt zapatos</strong></a>no es coincidencia que es tan popular. Esta es una forma de cambio histórico<a href="http://www.mbtzapatos2010.com/mens-mbt-zapatos-c-238_246_247.html"><strong>Mbt zapatos</strong></a>son.

Re: Silverlight 2.0 - Bug with Data Binding?  By wgsa on 2010-07-28 06:08:58Z
<a href="http://www.mbt-zapatos-2010.com"><strong>Mbt zapatos</strong></a>como una tormenta se extendió por Europa y el mundo.<a href="http://www.mbt-zapatos-2010.com/free-shipping-mbts-c-272.html"><strong>Mbt zapatos</strong></a>cómo tener tanto poder? Según la investigación,<a href="http://www.mbt-zapatos-2010.com/men-mbt-shoes-c-242.html"><strong>Mbt zapatos</strong></a>no sólo su propio estilo de diseño único, sino también porque hay magia buena forma física, así<a href="http://www.mbt-zapatos-2010.com/women-mbt-shoes-c-240.html"><strong>Mbt zapatos</strong></a>no es coincidencia que es tan popular. Esta es una forma de cambio histórico<a href="http://www.mbtzapatos2010.com/mens-mbt-zapatos-c-238_246_247.html"><strong>Mbt zapatos</strong></a>son.

Re: Silverlight 2.0 - Bug with Data Binding?  By wgsa on 2010-07-28 06:09:16Z
Mbt zapatos como una tormenta se extendió por Europa y el mundo. Mbt zapatos cómo tener tanto poder? Según la investigación, Mbt zapatos no sólo su propio estilo de diseño único, sino también porque hay magia buena forma física, así Mbt zapatos no es coincidencia que es tan popular. Esta es una forma de cambio histórico Mbt zapatos son.


Your name:
Title:
Comment:
Add Comment  Cancel 

Search Blog

Blog List

Blog Archive