Dynamically Creating Textboxes MVVM?
c#, mvvm, wpf
Solution
You can use
<ItemsControl ItemsSource="{Binding StandardCollection}">
<ItemsControl.ItemTemplate>
<DataTemplate DataType="{x:Type Standard}">
<Grid>
<TextBox Text={Binding Title} />
<ItemsControl ItemsSource="{Binding Questions}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBox Text={Binding} />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Which is binded to any collection in your viewModel.
You can add as many items as required in the collection.
Update:
public class Standard
{
string _title;
ObservableCollection<string> _questions;
public string Title
{
get { return _title; }
set {
_title = value;
NotifyOfPropertyChanged(()=>Title);
}
}
public ObservableCollection<string> Questions
{
get { return _questions; }
set {
_questions = value;
NotifyOfPropertyChanged(()=>Questions);
}
}
}
public class StandardViewModel
{
private ObservableCollection<Standard> _standardCollection;
public ObservableCollection<Standard> StandardCollection{
get
{
return _standardCollection;
}
set{
_standardCollection = value;
NotifyOfPropertyChanged(()=>StandardCollection);
}
}
}
Looking at your Diagram 1: Its seems you may have multiple questions for each title. So here is the solution.
Yes, You will need the `Standard` class to make it simple.
Sorry, I don't have Visual Studio right now, I just wrote this code in NotePad, and pasted here. Not sure about errors. But just are high level idea.
Problem
I can dynamically create Textbox's in C# code which I have achieved, but people have been saying I need to follow the MVVM pattern, I looked into it and it seems really hard and I just can't get used to it. I need to dynamically create text boxes, save the information in the text boxes to SQL and then be able to reopen it. Here is a picture describing what I need to do: Is this possible to do without using the MVVM pattern? Just need abit of a push start and explanation of how I can do this, I don't want to be supplied with all the code. EDIT1: I don't know if this is right. I have created a class called 'Standard' which looks like this: ``` namespace MVVModel { public class Standard { string _title; string _question; public string Title { get { return _title; } set { _title = value; } } public string Question { get { return _question; } set { _question = value; } } } } ``` Now I am going to create a ViewModel? What needs to be in this?