Pass typeof as parameter for command from xaml

I am creating an application with RibbonWindow and TabCollection.

Each RibbonButton has a command to open the tab of a specific UserControl. Each command does the same with very little difference, they open a tab with a specific UserControl. Is there a good way to pass this type of UserControl to a single command called OpenTabCommand?

This is what it looks like right now:

Xaml ...

<RibbonButton Label="OpenTab1"
              LargeImageSource="/something.png" 
              Command="{Binding OpenTab1Command}" />

<RibbonButton Label="OpenTab2"
              SmallImageSource="/something.png" 
              Command="{Binding OpenTab2Command}"/>

      

...

ViewModel

public RelayCommand OpenTab1Command{ get; set; }

public RelayCommand OpenTab2Command { get; set; }

public MainViewModel()
{
    OpenTab1Command= new RelayCommand(OpenTab1, param => true);

    OpenTab2Command = new SearchCommand(OpenTab2, param => true);
}

private void OpenTab1()
{
    var item = new TabItem
    {
        Content = new Tab1(),
    };

    TabCollection.Add(item);

    item.Focus();
}

private void OpenTab2()
{
    var item = new TabItem
    {
        Content = new Tab2(),
    };

    TabCollection.Add(item);

    item.Focus();
}

      

+3


source to share


1 answer


you can use CommandParameter

<RibbonButton Label="OpenTab1"
              LargeImageSource="/something.png" 
              Command="{Binding OpenTab1Command}" 
              CommandParameter="{x:Type (YOUR TYPE)}"/>

      



and make sure yours RelayCommand

takes a parameter in your handler.

+4


source







All Articles