Noobie3001
Noobie3001

Reputation: 1251

MVVM WPF: Get usercontrol name from ViewModel

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!

Upvotes: 2

Views: 1633

Answers (1)

Athari
Athari

Reputation: 34275

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.

Upvotes: 2

Related Questions