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.
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);
}
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.
Shawn Wildermuth
MVP, Speaker and Author
Web Workshop (HTML5/CSS/MVC4)
San Fran, CA - Mar 28-30, 2012
Dallas, TX: Apr 29-May 1, 2012
https://agilitrain.com/Workshop/Info/Web_Workshop
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
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.
JoeF___
Member
5 Points
11 Posts
DependencyProperty as data binding source
Jan 16, 2009 10:11 PM | LINK
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
Star
8438 Points
1547 Posts
Re: DependencyProperty as data binding source
Jan 17, 2009 01: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.
MVP, Speaker and Author
Web Workshop (HTML5/CSS/MVC4)
San Fran, CA - Mar 28-30, 2012
Dallas, TX: Apr 29-May 1, 2012
https://agilitrain.com/Workshop/Info/Web_Workshop
murtaza.dhar...
Participant
1659 Points
378 Posts
Re: DependencyProperty as data binding source
Jan 17, 2009 03: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
Diaspark Inc.
www.diaspark.com
email:murtaza.dharwala@diaspark.com
Please remember to click “Mark as Answer” on the post that helps you
andrejt
Participant
902 Points
141 Posts
Re: DependencyProperty as data binding source
Jan 17, 2009 08: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.
MVP - Client Application Development | Blog | Twitter
JoeF___
Member
5 Points
11 Posts
Re: DependencyProperty as data binding source
Jan 17, 2009 03: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.