I have an application in WPF that use the MVVM pattern and I use to have a list or observableCollection for example to binding comboBox, DataGrids... etc. However I have a control and I would like to add dynamically items of this control to the wrap pannel.
is a good idea in my view model create the view and view model of this control and add it to the list that is binding to the wrap pannel?
If this is not a good idea, how can I add items to the wrap pannel dynamically?
Thanks.
EDIT
I add code.
I am trying to do it but I don't get the solution.
I have a principal view model that create the view and the view model of my secondary view. In this principal view model I use this code:
ucDialogView myDialogView = new ucDialogView ();
ucDialogViewModel myDialogViewModel = new ucDialogViewModel ();
ucDialogView.DataContext = ucDialogViewModel;
The view model of my dialog is the following:
public class DialogViewModel : BaseViewModel
{
private ObservableCollection<ControlViewModel> _controls = new ObservableCollection<ControlViewModel>();
public ObservableCollection<ControlViewModel> Controls
{
get { return _myControls; }
set
{
_myControls = value;
base.RaisePropertyChangedEvent("Controls");
}
}
public myControlViewModel()
{
ControlViewModel myControlViewModel = new ControlViewModel();
Controls.Add(myControlViewModel);
}
}
My axml of my dialog:
<UserControl x:Class="MyApp.Views.DialogView"
Name="Principal"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:my="clr-namespace:myApp.ViewModels"
mc:Ignorable="d"
d:DesignHeight="1122" d:DesignWidth="794" Width="794" Height="1122">
<ItemsControl Grid.Row="1"
ItemsSource="{Binding Controls, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel HorizontalAlignment="Center" Margin="0,15,0,0" Name="wrpControls" VerticalAlignment="Stretch" Width="Auto" Grid.Row="1" IsItemsHost="True" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate DataType="{x:Type my:ControlViewModel}">
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</UserControl>
However, I get an empty dialog. Control is a user control, a view and its view model.