Furqan Safdar
Furqan Safdar

Reputation: 16718

Passing multiple parameters including Type to ViewModel in WPF

How to pass two parameters from XAML, one Type object and one Model {Binding}, to ViewModel as CommandParameter. I came across different posts on SO but all are using control bindings. Is there any way to pass Type instead.

I want something like this:

<MenuItem x:Key="RuleBase" Header="RuleBase" x:Shared="False" 
    Command="{Binding DataContext.AddRuleCommand, RelativeSource={RelativeSource AncestorType={x:Type Window}}}">
    <MenuItem.CommandParameter>
        <MultiBinding Converter="{StaticResource MultiParameterConverter}">
            <Binding Path="{Binding}" />
            <Binding Path="{x:Type local:RuleBase}" />
        </MultiBinding>
    </MenuItem.CommandParameter>
</MenuItem>

This piece of code is working with one parameter alone:

<MenuItem x:Key="RuleBase" Header="RuleBase" x:Shared="False" 
    Command="{Binding DataContext.AddRuleCommand, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"
    CommandParameter="{x:Type local:RuleBase}" />

Upvotes: 1

Views: 1320

Answers (2)

Ilan
Ilan

Reputation: 2782

Try to pass a whole MenuItem as a command parameter:

CommandParameter="{Binding RelativeSource={RelativeSource Self}}"

you must use an ICommand implementation that can take a parameter.

Upvotes: -1

Liero
Liero

Reputation: 27360

you can use this binding in the multibinding:

<MultiBinding Converter="{StaticResource MultiParameterConverter}">
    <Binding />
    <Binding Source="{x:Type local:RuleBase}" />
</MultiBinding>    

but since the Type won't change and there is only one true binding in the multibinding expression, it could be rewriten like this:

<MenuItem CommandParameter="{Binding ConverterParameter={x:Type local:RuleBase}, 
                                     Converter={StaticResource YourConverter}}" />

Upvotes: 2

Related Questions