Let us have a scenario where we need to render any User Control in Main UserControl on the basis of items present in a list.
- Main UserControl and MainViewModel
- Child UserControl and ChildViewModel.
XAML for ChildUserControl
- public class MainViewModel
- {
-
- private List<Service> serviceList;
-
-
- public string ServiceList
- {
- get
- {
- return serviceList;
- }
- set
- {
- if (serviceList != value)
- {
- serviceList = value;
- RaisePropertyChanged("ServiceList");
- }
- }
- }
-
-
-
- }
Now, let's render ChildUserControl in MainUserControl.
- <Grid>
- <ItemsControl Name="ControlCanvas" ItemsSource="{Binding ServiceList}">
- <ItemsControl.ItemsPanel>
- <ItemsPanelTemplate>
- <Canvas/>
- </ItemsPanelTemplate>
- </ItemsControl.ItemsPanel>
- <ItemsControl.ItemTemplate>
- <DataTemplate>
- <local:ChildUserControl DataContext="{Binding chilViewModel}"/>
- </DataTemplate>
- </ItemsControl.ItemTemplate>
- <ItemsControl.ItemContainerStyle>
- <Style TargetType="ContentPresenter">
- <Setter Property="Canvas.Height" Value="{Binding Anyproperty}"/>
- <Setter Property="Canvas.Width" Value="{Binding Anyproperty}"/>
- <Setter Property="Canvas.Left" Value="{Binding Anyproperty}"/>
- <Setter Property="Canvas.Top" Value="{Binding Anyproperty}"/>
- </Style>
- </ItemsControl.ItemContainerStyle>
-
- </ItemsControl>
- </Grid>
Now, as you can see in above block that we used childViewModel property to bind the ChildUserControl, so how do we get it in ViewModel? The MainViewModel should look like below.
- public class MainViewModel
- {
-
- private List<Service> serviceList;
-
-
- public List<Service> ServiceList
- {
- get
- {
- return serviceList;
- }
- set
- {
- if (serviceList != value)
- {
- serviceList = value;
- RaisePropertyChanged("ServiceList");
- }
- }
- }
-
- public class Service
- {
- public string Anyproperty { get; set; }
- public string serviceName { get; set; }
- public ChildViewModel chilViewModel { get; set; }
- }
-
- public void BindProperties()
- {
- Service serviceControl = new Service();
- serviceControl.Anyproperty = "1";
- serviceControl.serviceName = "test";
- serviceControl.chilViewModel = new ChildViewModel(serviceControl);
- ServiceList.Add(serviceControl);
- }
-
- }
And the childViewModel will be changed to,
- public class ChildViewModel
- {
- private string serviceName;
- private MainViewModel.Service serviceControl;
-
- public ChildViewModel(MainViewModel.Service serviceControl)
- {
-
- this.serviceControl = serviceControl;
- BindProperty();
- }
-
-
- public string ServiceName
- {
- get
- {
- return serviceName;
- }
- set
- {
- if (serviceName != value)
- {
- serviceName = value;
- RaisePropertyChanged("ServiceName");
- }
- }
- }
-
- public void BindProperty()
- {
- ServiceName = serviceControl.serviceName;
-
- }
-
- }
We do not need to do anything in ChildUserControl.cs page. And in mainUsercontrol.cs page, directly bind the datacontext using main ViewModel as below.
- public partial class MainUserControl : UserControl
- {
- public MainUserControl()
- {
- InitializeComponent();
- this.DataContext = new MainViewModel();
- }
- }
Feel free to ask questions on MVVM.
Thank you.