Question

I'm creating a kind of base WPF application to host WPF user controls, that will come in an assembly (later this will be AddIns). The application and the user control are following MVVM. Unfortunately I'm new to WPF and MVVM, so I am not that familiar with the specials. I have searched a lot, but nothing that helped me (or I didn't understand the solution, that might be possible).

So my application contains basic functionality for the user controls and a window that is split into a menu bar and the placeholder for the user control. This is what I have so far, there is a button to choose the VersionControl control, which will call a function in the viewModel of my MainWindow where I load the user control, but I do not get it to be shown in the 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>

The ViewModel definitions:

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

But what do I need to do in the OnLoadVersionControl function? I have the VersionControlView and VersionControlViewModel, but no idea how to get it shown in my application. Many thanks for your help,

Mike

Was it helpful?

Solution

I would use a RelayCommand and ICommand combo to bind to the XAML. Put the below into your ViewModel and don't forget to set the 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;
      }
 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top