【问题标题】:Dynamically set a UserControl in a WPF app with MVVM使用 MVVM 在 WPF 应用程序中动态设置 UserControl
【发布时间】:2012-11-16 14:25:49
【问题描述】:

我正在创建一种基础 WPF 应用程序来托管 WPF 用户控件,它将出现在程序集中(稍后将是 AddIns)。应用程序和用户控件遵循 MVVM。不幸的是,我是 WPF 和 MVVM 的新手,所以我对特价商品不是很熟悉。我进行了很多搜索,但没有任何帮助(或者我不理解解决方案,这可能是可能的)。

所以我的应用程序包含用户控件的基本功能和一个分为菜单栏和用户控件占位符的窗口。这是我到目前为止所拥有的,有一个按钮可以选择 VersionControl 控件,它将在我加载用户控件的 MainWindow 的 viewModel 中调用一个函数,但我没有让它显示在 MainWindow 中。

<Grid DataContext="{StaticResource Windows1ViewModel}">
        <Grid.RowDefinitions>
            <RowDefinition Height="50" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
    <Canvas Grid.Row="0"                >
        <Button Content="VersionControl"
                Style="{StaticResource ButtonStyle1}"
                HorizontalAlignment="Center"
                Command="{Binding LoadVersionControl}" />
    </Canvas>
    <Canvas Grid.Row="1">
        <ItemsControl Name="ControlCanvas" />
    </Canvas>
</Grid>

ViewModel 定义:

public ICommand LoadVersionControl { get { return new DelegateCommand(OnLoadVersionControl); } }

但是我需要在 OnLoadVersionControl 函数中做什么?我有 VersionControlView 和 VersionControlViewModel,但不知道如何在我的应用程序中显示它。 非常感谢您的帮助,

迈克

【问题讨论】:

  • Prism(来自 Codeplex)可以成为你的朋友。
  • 我在开始之前查看了 Prism,并认为我的方法有点过大。不过谢谢你的提示。

标签: wpf mvvm dynamic-usercontrols


【解决方案1】:

我会使用 RelayCommand 和 ICommand 组合来绑定到 XAML。 将以下内容放入您的 ViewModel 中,不要忘记设置 DataContext!

 // Execute method here
 private void LoadVersionControl(object param) {
      // do stuff here (if you are binding to same view Model for your MainWindow)
      //MainWindow.TextBoxInput.Visibility = Visibility.Visible
 }

 // Controls conditions to allow command execution
 private bool LoadVersionControlCanExecute(object param) { return true; }

 // Relay Command for method
 public RelayCommand _LoadVersionControl;

 // Property for binding to XAML
 public ICommand LoadVersionControlCommand {
      get {
           if(_LoadVersionControl == null) {
                _LoadVersionControl = new RelayCommand(LoadVersionControl, LoadVersionControlCanExecute);
           }

           return _LoadVersionControlCommand;
      }
 }

【讨论】:

  • 您好 Bob,我已经添加了代码,但不确定您对 DataContext 绑定的含义。因此,我在 MainWindowViewModel 中创建了一个 UserControl var,我将它绑定到 ItemsControl &lt;ItemsControl Name="ControlCanvas" DataContext="{Binding externalView}" /&gt; 的 DataContext,并在 LoadVersionControl 方法中将 externalView 设置为新的 VersionControlView。不幸的是,它不起作用。怎么了?
  • @Mike DataContext,我不是说绑定它,我是说为视图设置 DataContext。有时人们忘记设置它并询问为什么 View 没有链接到 ViewModel。它应该类似于View.DataContext = ViewModel;
  • 嗨,鲍勃,我不明白这应该如何工作。 VersionControlView 是一个空的 UserControl,它只包含一个标签来显示一些文本。但是在 LoadVersionControl 中,我现在添加了 Datacontext: externalView = new VersionControlView(); externalView.DataContext = new VersionControl.ViewModels.VersionControlViewModel(); 但是我觉得需要把externalView绑定到MainWindowView中的ItemControl上,不然应该怎么显示控件呢?
  • 好的,经过一番搜索,我看到有人通过使用 ContentControl 完成此操作,所以现在它看起来像这样:&lt;Canvas Grid.Row="1" &gt; &lt;ContentControl Name="ControlCanvas" Content="{Binding Path=externalView}"/&gt; &lt;/Canvas&gt; 现在它可以工作了:) 感谢您的帮助。
  • @Mike 这为我节省了一些打字时间!如果您要添加文本框、文本块、标签、组合框等,如果需要对齐,请查看 Grids、DockPanel 和 StackPanels!
猜你喜欢
  • 1970-01-01
  • 2017-07-14
  • 1970-01-01
  • 1970-01-01
  • 2015-10-25
  • 1970-01-01
  • 2010-10-21
  • 1970-01-01
  • 2020-01-26
相关资源
最近更新 更多