EDIT: Binding the data against multiple textboxes
After reading your comment, I will elaborate my example for textboxes.
First important thing is that the ViewModel will model the things in the View, so that the View gets all information it needs in the structure it needs. That means, if you have multiple textboses in the View, you will need multiple string Properties in your ViewModel, one for each textbox.
In your XAML you could have something like
<TextBox Text="{Binding ID, Mode=TwoWay}" /> <TextBox Text="{Binding SomeOtherData, Mode=TwoWay}" />
and in your ViewModel
public class TestUserControlViewModel : BaseViewModel { private string id; private string someOtherData; public TestUserControlViewModel() { DataItem firstItem = new DataRepository().GetData().First(); this.ID = firstItem.ID; this.SomeOtherData = firstItem.SomeOtherData; } public string ID { get { return this.id; } set { if (this.id == value) return; this.id = value; this.OnPropertyChangedEvent("ID"); } } public string SomeOtherData { get { return this.someOtherData; } set { if (this.someOtherData == value) return; this.someOtherData = value; this.OnPropertyChangedEvent("SomeOtherData"); } } }
Here I assume that in your BaseViewModel there is an OnPropertyChangedEvent method to fire the corresponding event. This tells the View that the property has changed and it must update itself.
Note the Mode=TwoWay in the XAML. This means, that it doesn't matter on which side the value changes, the other side will reflect the change immediately. So if the user changes a value in a TwoWay bound TextBox, then the corresponding ViewModel property will automatically change! And also vice versa: if you change the ViewModel property programmatically, the View will refresh.
If you want to show multiple textboxes for more than one data item, then you must introduce more Properties in the ViewModel and bind them accordingly. Maybe a ListBox with a flexible number of TextBoxes inside is a solution then, like @Haspemulator already answered.
Binding the data against a collection control
In the TestUserControl I guess you have a control (like a ListView) to show the list of loaded things. So bind that control against the list in the ViewModel with
<ListView ... ItemsSource="{Binding GetAllData}" ... />
First you must understand that Binding means not "read the data and then forget the ViewModel". Instead you bind the View to the ViewModel (and its Properties) as long as the View lasts. From this point of view, AllData is a much better name than GetAllData (thanks @Malcolm O'Hare).
Now in your code, every time the View reads the AllData property, a new DataRepository is created. Because of the Binding, that is not what you want, instead you want to have one instance of DataRepository for the whole lifetime of the View, which is used to read the initial data and can later be used to update the View, if the underlying database changes (maybe with an event).
To enable such a behavior you should change the type of the AllData property to an ObservableCollection, so that the View can automatically update the list if changes occur.
public class TestUserControlViewModel : BaseViewModel private ObservableCollection<DataItem> allData; public TestUserControlViewModel() { IGetTheData src = new DataRepository(); this.allData = new ObservableCollection<DataItem>(src.GetData()); } public ObservableCollection<DataItem> AllData { get { return this.allData; } } public void AddDataItem(DataItem item) { this.allData.Add(item); } }
Now if you call AddDataItem later, the ListView will update itself automatically.