2
votes

I'm new to MVVM so please excuse my idiocy.

I have a grid containing 6 instances of a custom usercontrol.

userCtrl1, userCtrl2, userCtrl3, userCtrl4, and so on..

I created a viewModel class called 'playerInfo' and set the usercontrol's datacontext.

My question is how to reference the name in the viewmodel.

I need to get the index ( userCtrl "4" ) to reference a list in a 3rd party library I'm working with.

Sounds wierd, and I don't know if this violates the MVVM pattern.

Many thanks in advanced!

1
To remain within MVVM, in the Xaml you can bind the Tag to a property with two-way binding, and in your VM you can populate it incrementally. Or alternatively use an Attached Property in your user control. There's two alternatives... - Gayot Fow

1 Answers

2
votes

Alternative solution (if "index to reference a list" is equal to userCtrl# number and layout is not too complex):

  1. Create an ObservableCollection<PlayerInfo> Players property in the view model.
  2. Put ItemsControl into your view and bind to Players. If you want to customize layout, you can change its ItemsPanel template.
  3. Create DataTemplate and set its DataType to {x:Type PlayerInfo}. Put your user control into it and bind to {Binding}.
  4. When you need index in the view model, you can use Players.IndexOf method.