1

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}" />
Furqan Safdar
  • 16,260
  • 13
  • 59
  • 93
  • So the first code block you posted didn't work for you? Since it looks alright to me. Except that you might want to remove the first `CommandParameter="{x:Type local:RuleSet}"` since you're defining that twice when you're using it together with ` ...`. Could you also post your MultiParameterConverter ? – Zerratar Oct 14 '15 at 06:23
  • Sorry there was the typo, i have corrected and removed the single command parameter from first code block. – Furqan Safdar Oct 14 '15 at 06:26
  • No `{Binding}` is referring to the Model that is associated with `MenuItem`. – Furqan Safdar Oct 14 '15 at 06:27
  • @FSX what's wrong with the first block of code you posted. Seems to me like a valid solution ... did it not work ? if so post your command AddRuleCommand and MultiParameterConverter – eran otzap Oct 14 '15 at 07:01

2 Answers2

2

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}}" />
Liero
  • 25,216
  • 29
  • 151
  • 297
  • Thanks but `` also giving syntax error. How to overcome it. – Furqan Safdar Oct 14 '15 at 06:56
  • just removed 1 space to make it shorter, it's a pity that it cannot be shorter (I don't mean this answers the OP's question - in fact really want to downvote this). – King King Oct 14 '15 at 06:56
  • @FSX: I have not used binding in Source property. – Liero Oct 14 '15 at 07:02
  • @Liero, I didn't get your second point that Type won't change and there is only one true binding in the multibinding expression. What does that mean? – Furqan Safdar Oct 14 '15 at 07:26
  • In my first code sample, there is binding to the DataContext and another binding to the type. But you dont have to bind to the type, because `RuleBase` will be still the same `RuleBase`. The second binding will never change the final value. So the Type can be passed as a ConverterParameter to a converter as I did in my second code sample. However DataContext can change (or can be different in different places). So there is only one binding, that makes sense. – Liero Oct 14 '15 at 07:45
-1

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.

Ilan
  • 2,762
  • 1
  • 13
  • 24