I’ve started an MVVM project and now I’m stucking with correct DataBinding.
My project has:
A UserControl whit a ViewModel as DataContext like:
public partial class TestUserControl: UserControl
{
public TestUserControl()
{
this.DataContext = new TestUserControlViewModel();
}
}
ViewModel code is (BaseViewModel class contains PropertyChangedEventHandler):
public class TestUserControlViewModel : BaseViewModel
{
public KrankenkasseControlViewModel()
{}
public IEnumerable<DataItem> GetAllData
{
get
{
IGetTheData src= new DataRepository();
return src.GetData();
}
}
}
IGetTheData is the interface to DataContext:
public interface IGetTheData
{
IEnumerable<DataItem> GetData();
}
}
and finally the DataRepository code:
public class DataRepository : IGetTheData
{
private TestProjectDataContext dax = new TestProjectDataContext();
public IEnumerable<DataItem> GetData()
{
return (from d in this.dax.TestData
select new DataItem
{
ID = d.ID,
SomeOtherData = d.SomeOtherData
});
}
}
My UserControl has a few TextBoxes, but what’s the best way to bind correctly?
Thanks for your help, regards.
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
and in your ViewModel
Here I assume that in your
BaseViewModelthere is anOnPropertyChangedEventmethod to fire the corresponding event. This tells the View that the property has changed and it must update itself.Note the
Mode=TwoWayin 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 aTwoWayboundTextBox, 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
ListBoxwith a flexible number ofTextBoxes inside is a solution then, like @Haspemulator already answered.Binding the data against a collection control
In the
TestUserControlI guess you have a control (like aListView) to show the list of loaded things. So bind that control against the list in the ViewModel withFirst 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,
AllDatais a much better name thanGetAllData(thanks @Malcolm O’Hare).Now in your code, every time the View reads the
AllDataproperty, a newDataRepositoryis created. Because of the Binding, that is not what you want, instead you want to have one instance ofDataRepositoryfor 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
AllDataproperty to anObservableCollection, so that the View can automatically update the list if changes occur.Now if you call
AddDataItemlater, the ListView will update itself automatically.