Powered by MSDN

US - English
NEW! Silverlight 5 is available Learn More

Data Binding broken in SLv2 Beta2 RSS

3 replies

Last post Sep 04, 2008 07:04 PM by shacktoms

(0)
  • atm_grifter

    atm_grifter

    Member

    8 Points

    29 Posts

    Data Binding broken in SLv2 Beta2

    Jul 16, 2008 09:07 PM | LINK

    Could someone explain why the following does not work:

     <UserControl
        x:Class="SilverlightApplication3.Page"
        Width="640" Height="480" xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows">

        <StackPanel x:Name="LayoutRoot" Background="White" >
            <Slider x:Name="slider1"/>
            <Slider x:Name="slider2"/>
        </StackPanel>
    </UserControl>

        public partial class Page : UserControl
        {
            public Page()
            {
                // Required to initialize variables
                InitializeComponent();

                Binding binding = new Binding("Value");
                binding.Source = slider1;
                binding.Mode = BindingMode.OneWay;
                slider2.SetBinding(Slider.ValueProperty, binding);
            }
        }
     

    As is, this does not work (but does in WPF).  To add to the strangeness, if I change the binding mode to TwoWay, then when you change the value of one slider the other one will update as expected, but if you change the other slider the other slider's value will not update.  So, the TwoWay binding works like the OneWay binding in WPF.  Seems like a pretty huge bug to me.


  • Yi-Lun Luo - MSFT

    Yi-Lun Luo -...

    All-Star

    25149 Points

    2759 Posts

    Microsoft

    Re: Data Binding broken in SLv2 Beta2

    Jul 18, 2008 08:24 AM | LINK

    Hello, I think this is because currently DependencyProperty doesn't support property changed notification as in WPF (which is by design). So when slider1's value changes, slider2 won't be notified. Two way binding is quite different. It doesn't require property changed notification. So a workaround is to create two two way bindings:

    Binding binding = new Binding("Value");
    binding.Source = slider1;
    binding.Mode = BindingMode.TwoWay;
    slider2.SetBinding(Slider.ValueProperty, binding);

    Binding binding2 = new Binding("Value");
    binding2.Source = slider2;
    binding2.Mode = BindingMode.TwoWay;
    slider1.SetBinding(Slider.ValueProperty, binding2);

    shanaolanxing - I'll transfer to the Windows Azure team, and will have limited time to participate in the Silverlight forum. Apologize if I don't answer your questions in time.
  • atm_grifter

    atm_grifter

    Member

    8 Points

    29 Posts

    Re: Data Binding broken in SLv2 Beta2

    Jul 18, 2008 03:46 PM | LINK

     Yes, I also discovered this solution.  I just wanted to make sure this is "by design".  While I see that you've at least documented the expected behavior in MSDN, could someone please explain to me why Silverlight is doing this?  I have to assume it's to keep the size of the Silverlight run-time at a minimum, but it's hard for me to imagine that excluding this feature (as implemented in WPF) was necessary.

     Also, can someone explain to me how to achieve in XAML what is done in the code above?  As far as I know it's impossible (at least without jumping through some extra, and very unwanted hoops).
     

  • shacktoms

    shacktoms

    Member

    185 Points

    155 Posts

    Re: Data Binding broken in SLv2 Beta2

    Sep 04, 2008 07:04 PM | LINK

    You can do it with the creation of one custom class, so that is one hoop, but the hoop doesn't know about anything in your Xaml...

    public class DoubleCache : INotifyPropertyChanged {
        public event PropertyChangedEventHandler PropertyChanged;
        private double _Value;
        public double Value {
            get { return _Value; }
            set {
                _Value = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("Value"));
                }
            }
        }
    

    Then you can set up the actual bindings in Xaml...

    <UserControl x:Class="TwoSliders.Page"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:local="clr-namespace:TwoSliders;assembly=TwoSliders"
                 Width="400" Height="300">
        <StackPanel x:Name="LayoutRoot" Background="White">
            <StackPanel.Resources>
                <local:DoubleCache x:Key="SliderPosition"/>
            </StackPanel.Resources>
            <Slider Value="{Binding Value, Mode=TwoWay, Source={StaticResource SliderPosition}}"/>
            <Slider Value="{Binding Value, Mode=TwoWay, Source={StaticResource SliderPosition}}"/>
        </StackPanel>
    </UserControl>