Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HI,
I have MainWindow and 10 User Controls. I am showing usercontrols on MainWindow. How would i know which usercontrol is present in Mainwindow.

Thanks.
Posted

1 solution

If you are following mvvm design pattern there are such steps:
1. In your userControl model create a property and call it ControlId.
2. Add userControls to some variable: for example List or ConcurrentBag or whatever.
3. Bind this to your view(MainWIndow).
4. Implement Event on your MainWIndowViewModel to check for userControls.

Example:
YourUserControlModel.cs :
C#
public int ControlId{get;set;}

Set somewhere this property.
Load your usercontrols in constructor like this:
C#
public MainWindowViewModel()
{
    Controls.Add(new YourUserControl());
}

private readonly DeferrableObservableCollection<YourUserControl> Controls = new DeferrableObservableCollection<YourUserControl>();

Event to look for userControls:
C#
void SomeName(object Sender, EventArgs e)
{
   foreach(var control in Controls)
   {
       if(control.ViewModel.Model.ControlId == 1) // checks, if collection has userControl with id 1. And so on for all controls
       
   }
}

Hope this helped you.
If you have questions feel free to ask.
 
Share this answer
 
v4
Comments
Subbarayudu k 19-Dec-13 9:16am    
I am not using MVVM model
Alexander Dymshyts 19-Dec-13 10:51am    
Do you add controls during runtime?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900