2

我正在创建一种基础 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,但不知道如何在我的应用程序中显示它。非常感谢您的帮助,

麦克风

4

1 回答 1

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;
      }
 }
于 2012-11-16T14:35:42.800 回答