I have a basic WPF/Silverlight user control code which includes a label that I want to set the value of from code which uses the control. Is there a way to simplify the requirements for definition of the dependency property and related events? It seems very noisy for what appears to be a simple coding task (a property, a method, and related wiring).
private static DependencyProperty CountProperty;
public MyWpfUserControl()
{
InitializeComponent();
PropertyChangedCallback countChangedCallback = CountChanged;
var metaData = new PropertyMetadata(countChangedCallback);
CountProperty = DependencyProperty.Register("Count", typeof (int), typeof (MyWpfUserControl), metaData);
}
public int ItemsCount
{
get { return (int) GetValue(CountProperty); }
set { SetValue(CountProperty, value); }
}
private void CountChanged(DependencyObject property,
DependencyPropertyChangedEventArgs args)
{
// Set the value of another control to this property
label1.Content = ItemsCount;
}
You are right for sure that dependency properties are ugly and clumsy to work with. In fact, in your code example above, there are even bugs! You need to call the doctor -- Dr. WPF!
Here are Dr. WPF's snippets for all the dependency property flavors you desire:
There are also videos on his site showing him using them. Honestly I don't use them myself but I've been meaning to try them out. I do use the built in snippets.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With