2011-04-09 89 views
0

我试图使用<MultiBinding>将两个参数传递给ViewModel的命令,但遇到问题让XAML解析器接受我的尝试。在MultiBinding命令参数中包含绑定对象

请考虑以下列表视图,该视图包含在我的UserControl中,它绑定到一组Ticket对象。

<ListView x:Name="lvTicketSummaries" 
        ItemsSource="{Binding TicketSummaries}" 
        ItemContainerStyle="{DynamicResource ResourceKey=ListViewItem}" 
        IsSynchronizedWithCurrentItem="True"> 

各自的风格ListViewItem

<Style x:Key="ListViewItem" TargetType="{x:Type ListViewItem}"> 
    <Setter Property="ContextMenu" Value="{DynamicResource ResourceKey=cmListViewItem}"/> 
    <!-- A load of irrelevant stuff -> 
</Style> 

和引用ContextMenu项目在我的查询的源坐镇;

<!-- ContextMenu DataContext bound to UserControls view model so it can access 'Agents' ObservableCollection -->  
<ContextMenu x:Key="cmListViewItem" DataContext="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl}}, Path=DataContext}"> 
    <MenuItem Header="Send as Notification" ItemsSource="{Binding Path=Agents}"> 
     <MenuItem.ItemContainerStyle> 
      <Style TargetType="{x:Type MenuItem}"> 
       <!-- Display the name of the agent (this works) --> 
       <Setter Property="Header" Value="{Binding Path=Name}"/> 
       <!-- Set the command to that of one on usercontrols viewmodel (this works) --> 
       <Setter Property="Command" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}, Path=DataContext.SendTicketNotification}" /> 
       <Setter Property="CommandParameter"> 
        <Setter.Value> 
         <MultiBinding Converter="{StaticResource ResourceKey=TicketNotificationParameterConverter}"> 
          <MultiBinding.Bindings> 
           <!-- This SHOULD be the Agent object I clicked on to trigger the Command. This does NOT work, results in either exception or 'UnsetValue' --> 
           <Binding Source="{Binding}" /> 
           <!-- Also pass in the Ticket item that was clicked on to open the context menu, this works fine --> 
           <Binding RelativeSource="{RelativeSource AncestorType={x:Type ListView}}" Path="SelectedItem" /> 
          </MultiBinding.Bindings> 
         </MultiBinding> 
        </Setter.Value> 
       </Setter> 
      </Style> 
     </MenuItem.ItemContainerStyle> 
    </MenuItem> 
</ContextMenu> 

所以这里是我试图做的;

  • 上下文菜单中有一个单一的项目“发送票据为通知”的选择时,列出了所有可用Agents可以接收所述通知。这工作。

  • 当我点击这些代理选一个,我想上下文菜单项,同时发送的ticket项目被点击从listview,要显示快捷菜单(这工作),我从选择的Agent项目上下文菜单。我虽然已经在上下文菜单中的实际的建立似乎有些什么复杂,我与MultiBinding

    <MultiBinding Converter="{StaticResource ResourceKey=TicketNotificationParameterConverter}"> 
             <MultiBinding.Bindings> 
              <!-- This works, it sends the Ticket object to the Converter --> 
              <Binding RelativeSource="{RelativeSource AncestorType={x:Type ListView}}" Path="SelectedItem" /> 
              <!-- This caused an exception saying that I can only set 'Path' property on DependencyProperty types --> 
              <Binding Path="{Binding}" /> 
             </MultiBinding.Bindings> 
            </MultiBinding> 
    

现在半实现了这一点。 MultiBinding参数之一的实际规格应该是绑定到点击的ContextMenu.MenuItem的实际项目,这似乎是一个非常简单的命令。上下文菜单正确地列出了所有Agent,所以我认为只需要简单地要求将当前对象作为命令参数发送。我也尝试过;

<Binding RelativeSource={RelativeSource AncestorType={x:Type MenuItem}} Path="SelectedItem" /> 

以及

<Binding RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}} Path="SelectedItem" /> 

但所有的被发送到参数转换为UnsetValue

谢谢你的时间,你可能有任何建议。

回答

3

此行是奇数:

<Binding Path="{Binding}" /> 

是当前的DataContext这就够了,因为这将查找当前的DataContext这实际上是不是字符串毕竟东西路径的字符串?非常有意义。

你是不是要这样做,而是绑定到DataContext?

<Binding />