I am new to WPF and MVVM and trying to follow this design, I have created a window with multiple user controls (10 of each) on it. These user controls will hold a value that should be able to be entered by the User and sent back to the Database.
The issue I have is I am creating the User Controls Pragmatically in a canvass and do not know how to use these instances to set the values on the control from my View Model where I have a SaveMethod that is binded to a Save Button to save the data into the Database. Thanks for the help.
MainWindow.xaml.cs
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
ClientRatesViewModel viewModel = new ClientRatesViewModel();
DataContext = viewModel;
viewModel.GetChargeUnits();
int previousTopPreRate = 10;
foreach (var rate in viewModel.ClientRatesPreAwr)
{
PreAwr preAwr = new PreAwr();
preAwr.tbPreAwrRate.Text = rate.ClientRatesPreAwr;
PreRatesCanvas.Children.Add(preAwr);
preAwr.Width = 500;
Canvas.SetLeft(preAwr, 10);
Canvas.SetTop(preAwr, previousTopPreRate + 10);
previousTopPreRate += +30;
}
int previousTopPostRate = 10;
foreach (var rate in viewModel.ClientRatesPostAwr)
{
PostAWR postAwr = new PostAWR();
postAwr.tbPostAwrRate.Text = rate.ClientRatesPostAwr;
PostRatesCanvas.Children.Add(postAwr);
postAwr.Width = 500;
Canvas.SetLeft(postAwr, 10);
Canvas.SetTop(postAwr, previousTopPostRate + 10);
previousTopPostRate += +30;
}
}
}
ItemsControl XAML:
<ItemsControl Name="icPreAwr" Margin="10,46,10,10">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid Margin="0,0,0,5">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="100" />
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding ClientRatesPreAwr }" />
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>