c# - How to distinguish similar UserControls in ViewModel? -
WP8Ap I have XML page with the same user controls, for this moment it seems pretty easy:
& lt; TextBlock text = "name" HorizontalAlignment = "left" verticalAlignment = "top" /> & Lt; Checkbox content = "chk1" horizontal alignment = "left" vertical alignment = "bottom" margin = "0,0,0,0" /> & Lt; Checkbox content = "chk2" horizontal alignment = "left" vertical element = "bottom" margin = "119.0,0,0" /> I want to store this data in the collection of objects. Now I try to use data contact and iCommond interface. It is stupid to make 12 assets for every one control, so the question is how can I send information of datacontact, about which a user control is still working? It will be easy to use for user control or index of index.
Im just learning about MVVM ... any advice? Text after "
" itemprop = "text"> are two ways to approach it: data-reference legacy, or dependency properties.
The first method will be to use the fact that the control of the child will be with the parent of the datacentext. Suppose you have a main visual-model with a sub-view-model, then you can force each user control into a proper view-model. Example
& lt; Local: MyUserControl DataContext = "{Binding FirstSubViewModel}" /> & Lt; Local: MyUserControl DataContext = "{Binding SecsviewView Model}" /> Your user control just assumes that your DataContext is all the ICommand property needs it.
The second approach will be to control your user defined type ICommand : public static read-only DependencyProperty Command1Property = DependencyProperty.Register ("Command1", typeof (ICommand), typeof (MyUserControl), zero); Public Identmand Command1 {Return (ICM) Gateville (Command1 Property); } Set {SetValue (Command1Property, value); }} In this way, the users of your control will be able to force the commands using any compulsive method. Example:
& lt; Local: MyUserControl Command1 = "{binding chk1Command}" /> & Lt; Local: MyUserControl Command1 = "{binding element name = some control, path = some command}" /> The only trick here is to force you to somehow relieve the dependencies from within your user control - this is difficult in the lightlight, because there is no because there are RelativeSource = FindAncestor Binding One way to do this is to have the control to set the data connection of Layouts. So if your manufacturer is like this: & lt; UserControl ... & gt; & Lt; Grid X: Name = "Layouts" & gt; & Lt; Text Block Text = "Name" Horizontal Alignment = "Left" Vertical Element = "Head" /> & Lt; Checkbox content = "chk1" horizontal alignment = "left" vertical alignment = "bottom" margin = "0,0,0,0" /> & Lt; Checkbox content = "chk2" horizontal alignment = "left" vertical element = "bottom" margin = "119.0,0,0" /> & Lt; / Grid & gt; & Lt; / UserControl & gt; Then in the manufacturer, you will:
Public partial class MyUserControl: UserControl {public MyUserControl () {InitializeComponent (); LayoutRoot.DataContext = This; }}
Comments
Post a Comment