0
votes

I have a Window that displays a variable number of identical UserControls which contain (among other things) a TextBox. A single ViewModel serves both the window and the UserControls. How do I bind the UserControl's TextBox's Text property to an element of a List-of-strings in my view model when the required index is passed to the constructor of the UserControl?

I also need Mode="TwoWay" and UpdateSourceTrigger="PropertyChanged".

Using VS2013, .NET 4.5.2, and MVVM-Light framework.

Thank you.

2
Why don't you use an extra property for the instead of hacking around trying to bind a List<string> to a string property? – Jannik

2 Answers

3
votes

You would need to create a new property in your ViewModel that would act as a "wrapper" for that call.

public string MyElementItem {
   get { return myStringList[myAwesomeIndexFromConstructor]; }
   set { 
      myStringList[myAwesomeIndexFromConstructor] = value; 
      RaisePropertyChanged(...);
}

And then bind to this new property and it should do the magic for you!

<TextBox Text="{Binding MyElementItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
0
votes

Decided to create a new ViewModel class to serve just the UserControls, one per object. Same class that creates the UserControls now creates the ViewModels and passes one in the UserControls ctor.