使用MVVM是一个很好的做法,添加控件来从视图模型中包装面板?

时间:2013-04-03 07:12:01

标签: wpf mvvm wrappanel

我在WPF中有一个使用MVVM模式的应用程序,我使用了一个list或observableCollection,例如绑定comboBox,DataGrids ......等等。但是我有一个控件,我想动态添加这个项目控制包裹面板。

在我的视图模型中是个好主意创建此控件的视图和视图模型并将其添加到绑定到换行盘的列表中?

如果这不是一个好主意,我怎样才能动态地将项目添加到换行盘?

感谢。

修改

我添加了代码。

我正在尝试这样做,但我没有得到解决方案。

我有一个主视图模型,用于创建我的辅助视图的视图和视图模型。在这个主视图模型中,我使用以下代码:

ucDialogView myDialogView = new ucDialogView ();
ucDialogViewModel myDialogViewModel = new ucDialogViewModel ();
ucDialogView.DataContext = ucDialogViewModel;

我的对话框的视图模型如下:

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);
    }

}

我的对话框:

<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>

但是,我得到一个空对话框。控件是用户控件,视图及其视图模型。

1 个答案:

答案 0 :(得分:3)

我认为更好的方法可能是为您要使用的每个ViewModel类型定义DataTemplate。然后,您可以将ViewModel对象本身添加到WrapPanel的项目中 WPF基础结构将看到这种类型的对象有一个DataTemplate,它将负责显示相关的DataTemplate。

您需要在WrapPanel可访问的某个范围内声明DataTemplate,例如在父页面/窗口的资源或App.xaml的资源中:

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

请注意,WrapPanel没有ItemsSource属性,因此如果要将其项目绑定到对象列表(例如ViewModel),则需要使用{{ 1}}:

ItemsControl

假设<ItemsControl ItemsSource="{Binding YourListOfViewModels}"> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <WrapPanel/> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> </ItemsControl> 是ViewModel的YourListOfViewModels,每次将ViewModel添加到该列表时,都会显示正确的ObservableCollection

相关问题