Powered by MSDN

US - English
NEW! Silverlight 5 is available Learn More

DependencyProperty as data binding source RSS

4 replies

Last post Jan 17, 2009 04:51 PM by JoeF___

(0)
  • JoeF___

    JoeF___

    Member

    5 Points

    11 Posts

    DependencyProperty as data binding source

    Jan 16, 2009 11:11 PM | LINK

    Hello,

    I have a quick question about data binding with a DependencyProperty as the source.

    The documentation on the DependencyObject class (link) states the primary function of the property system is to provide notification about values that have changed.

    Also, I understand binding to DPs to be the most efficient form of data binding in WPF.

    So, I’m trying to bind UI elements to custom DependencyProperties in custom data objects in Silverlight. But, updates to the source property are not propagated to the target property on bound controls.

    Below is some sample code.

    Are DepenencyProperties not viable sources for data binding where the data changes? Is INotifyPropertyChanged the main way to accomplish this?

    I checked the section ‘Differences between the Silverlight and WPF Property Systems’ section (link), but I missed info pertaining to this.

    Can someone please clarify?

    Thanks,
    Joe



    <StackPanel x:Name="LayoutRoot" Background="White" Margin="10">
            <Button Content="Increment value" Click="IncrementButton_Click" Margin="0,0,10,0" Width="120" HorizontalAlignment="Left" />
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="DP current value: " Width="150" />
                <TextBlock x:Name="DPTextBlock" Text="{Binding Counter}" />
            </StackPanel>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="INotify current value: " Width="150" />
                <TextBlock x:Name="INotifyTextBlock" Text="{Binding Counter}" />
            </StackPanel>
        </StackPanel>


    public class DPCounterHolder : DependencyObject
        {
            #region DependencyProperty int Counter
            /// <summary>
            /// Gets or sets the Counter property.
            /// </summary>
            public int Counter
            {
                get { return (int)GetValue(CounterProperty); }
                set { SetValue(CounterProperty, value); }
            }

            /// <summary>
            /// Identifies the Counter dependency property.
            /// </summary>
            public static readonly DependencyProperty CounterProperty =
                    DependencyProperty.Register(
                            "Counter",
                            typeof(int),
                            typeof(DPCounterHolder),
                            new PropertyMetadata(0));

            #endregion public int Counter
        }

        public class INotifyCounterHolder : INotifyPropertyChanged
        {
            protected int m_counter = 0;
            public int Counter
            {
                get { return m_counter; }
                set
                {
                    m_counter = value;
                    NotifyPropertyChanged("Counter");
                }
            }
            public event PropertyChangedEventHandler PropertyChanged;
            public void NotifyPropertyChanged(string propertyName)
            {
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }

        }

        public partial class Page : UserControl
        {

            public DPCounterHolder DPCounter { get; set; }
            public INotifyCounterHolder INCounter { get; set; }
            public Page()
            {
                InitializeComponent();
                this.Loaded += new RoutedEventHandler(Page_Loaded);
            }

            void Page_Loaded(object sender, RoutedEventArgs e)
            {
                DPCounter = new DPCounterHolder();
                INCounter = new INotifyCounterHolder();
                this.DPTextBlock.DataContext = DPCounter;
                this.INotifyTextBlock.DataContext = INCounter;
            }

            private void IncrementButton_Click(object sender, RoutedEventArgs e)
            {
                DPCounter.Counter++;
                INCounter.Counter++;
            }
        }

  • swildermuth

    swildermuth

    Star

    8438 Points

    1547 Posts

    Re: DependencyProperty as data binding source

    Jan 17, 2009 02:46 AM | LINK

    I think you have the idea of DependencyProperties backwards. Your custom objects don't need to expose DP's. Your UI controls do though. If you support the INotifyPropertyChange, it should just work.

    (If this has answered your question, "Mark as Answer")

    Shawn Wildermuth
    C# MVP, MCSD, Speaker and Author

    Silverlight 3 Workshop
    Miami, FL: Oct 12-14th
    Portlant, OR: Dec 2-4th
    Atlanta, GA: Dec 7-9th
    http://silverlight-tour.com
  • murtaza.dharwala

    murtaza.dhar...

    Participant

    1659 Points

    378 Posts

    Re: DependencyProperty as data binding source

    Jan 17, 2009 04:48 AM | LINK

    I guess the concept of the dependency property is a bit different

    Controls are supposed to have these dependency properties, and databinding is supported to those properties of controls which are dependency property.

    the use of INotifyChange is necessary because u want the system to know about change in object.

    when a object is changed the dependency property binded to the object gets notified, and the dependency property updates itself with new value,

    you dont have to write the code of updating a property

    In terms of code

    when we provide implementation of INotifyPropertyChanged

    we do no have to manually write the event of handling the property change in dependency properties

    Murtaza Dharwala
    Diaspark Inc.
    www.diaspark.com
    email:murtaza.dharwala@diaspark.com


    Please remember to click “Mark as Answer” on the post that helps you
  • andrejt

    andrejt

    Participant

    902 Points

    141 Posts

    Re: DependencyProperty as data binding source

    Jan 17, 2009 09:56 AM | LINK

    This looks like one of the differences with DPs between WPF and Silverlight. Your posted solution works in WPF, but doesn't in Silverlight.

    I'm not aware of any official documentation on this different behavior, but if you take a look at DependencyObject metadata, you'll see that in WPF it implements the OnPropertyChanged method, which Silverlight's DO doesn't. Haven't looked at the internals yet, but this may indicate the cause for observed (mis)behavior.

    In Silverlight, however, INotifyPropertyChanged is the way to go with such properties.

    Andrej Tozon
    MVP - Client Application Development | Blog | Twitter
  • JoeF___

    JoeF___

    Member

    5 Points

    11 Posts

    Re: DependencyProperty as data binding source

    Jan 17, 2009 04:51 PM | LINK

    Thanks for the replies.

    I think I got my wires crossed in reading about SL and WPF. There isn't a whole lot of documention on SL out, so I was reading them both and fell in love with DOs and their DPs. lol, Goes to show that you have to wait until you really get to know a feature before you fall in love with it...

    A side note: another issue I've run into with DOs in SL is that you can't bind a collection of DOs to ItemsSource on an ItemsControl. This has been noted by MSFT as a bug.