Question

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.

Was it helpful?

Solution

I think a better approach might be to define a DataTemplate for each ViewModel type you want to use. Then, you can just add the ViewModel objects themselves to the WrapPanel's items.
The WPF infrastructure will see there is a DataTemplate for this type of object and will take care of showing the associated DataTemplate.

You need to declare the DataTemplate in some scope that is accessible to your WrapPanel, for example in the Resources of a parent page/window or in App.xaml's Resources:

<DataTemplate DataType="{x:Type local:YourViewModelClass}">
  <ComboBox>
    <!-- Define your bindings and stuff here -->
  </ComboBox>
</DataTemplate>

Note that WrapPanel doesn't have an ItemsSource property, so if you want to bind its items to a list of objects (like your ViewModels), you'll need to use an ItemsControl:

<ItemsControl ItemsSource="{Binding YourListOfViewModels}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
</ItemsControl>

Assuming YourListOfViewModels is an ObservableCollection of your ViewModels, each time you add a ViewModel to that list, it will be displayed with the correct DataTemplate.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top