Custom dependency property WPF user control in MVVM pattern using a cheat

Dependency properties equivalent to expanding the  WPF  list of the original property of the label, and you can use WPF binding function, can be described as very convenient; user controls a way of code reuse is equivalent; these points separately is quite easy to understand , but to use MVVM pattern, or to explore some of the.

We first create a user control (UC_FoodsPanel.xaml), which put a StackPanel:

 

Dependent attributes related content online reference " WPF series - controls add dependency properties - looking over towards - blog Park ," a text, add a method relies property is - input propdp then double-click the tab key.

 The above code, we add a dependency property to Items, is a list of UC_FoodItem class. The key is that when a callback function PropertyChangedCallback property changes, which control parameter represents obj binding properties, i.e. UC_FoodsPanel here, args parameter and there OldValue NewValue, represent the values ​​before and after the attribute change. I.e. where the change takes the value - a list - assigned to user control StackPanel.

 After this, we use this user control in other pages (Views \\ MainWindowView.xaml) when you can use the Items property of:

XHTML

 

Binding properties using the ViewModel INotifyPropertyChanged mode here is the use of the frame written Prism (VM BindableBase inherited class). Here then noticed a new local variable items, fill only complete data assigned to Items, this is not superfluous, because it is not so, change the state of the property (PropertyChangedCallback) can not be triggered.

 

Last but not least is: Since the user controls are so simple, why does not directly depend on the contents inside, including the use of property in the page write it? Because then, due to the MVVM

The reason mode, DataContext page has been designated as the associated ViewModel, then write in the background of the DataContext dependency property can not find.

Guess you like

Origin www.cnblogs.com/lsgsanxiao/p/11330525.html