Is there a way to simplify setting dependency properties in WPF and Silverlight?

I have a basic WPF / Silverlight user control code that includes the label that I want to set for the value from the code that uses the control. Is there a way to simplify the requirements for defining dependency properties and related events? It seems very noisy for what seems like a simple coding task (property, 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; } 
+6
source share
1 answer

You are sure that dependency properties are ugly and inconvenient to work with. In fact, there are even errors in the above code example! You need to call a doctor - a WPF doctor!

Here are fragments of Dr. WPF for all the desires of the dependency properties you desire:

His website also has a video showing how he uses them. Honestly, I do not use them myself, but I mean try them. I do use inline snippets.

+1
source

Source: https://habr.com/ru/post/886289/


All Articles