2015-10-14 48 views
1

如何将两个参数从XAML(一个Type对象和一个Model {Binding})传递给ViewModel作为CommandParameter。我遇到过不同的帖子,但都使用控制绑定。有没有什么方法可以传递Type。将多个参数(包括类型)传递给WPF中的ViewModel

我想是这样的:

<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> 

这段代码正在与单独一个参数:

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

那么你发布的第一个代码块并不适合你?因为它看起来对我很好。除了你可能想要删除第一个'CommandParameter =“{x:Type local:RuleSet}”',因为当你将它与' ...一起使用时,你定义了两次。你还可以发布你的MultiParameterConverter吗? –

+0

对不起,有错字,我已经纠正并从第一个代码块中删除单个命令参数。 –

+0

没有'{Binding}'指的是与'MenuItem'关联的模型。 –

回答

2

您可以使用此绑定在multibinding:

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

但由于Type不会改变,并且在多重绑定表达式中只有一个真正的绑定,所以它可以被重写像这样:

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

感谢但'也给语法错误。如何克服它。 –

+0

只是删除了1个空格,使其更短,可惜它不能更短(我不是说这回答了OP的问题 - 实际上是真的想降低这个)。 –

+0

@FSX:我没有在Source属性中使用绑定。 – Liero

-1

试图通过整个菜单项为命令参数:

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

你必须使用一个ICommand的实现,可以采取的参数。