在MultiBinding命令参数中包含对象绑定



我试图使用<MultiBinding>传递两个参数到我的ViewModel的命令,但我有问题让XAML解析器接受我的尝试。

考虑下面的ListView包含在我的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可以接收该通知。这工作。

  • 当我点击这些代理选项之一时,我希望上下文菜单项发送从listview点击的ticket项以显示上下文菜单(这是有效的)和我从上下文菜单中选择的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的实际项的实际规范似乎是一个非常简单的命令。上下文菜单正确地列出了所有代理,因此我认为简单地请求将当前对象作为命令参数发送,非常简单。我也试过;

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

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

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

感谢您的宝贵时间和宝贵意见。

这一行是奇数:

<Binding Path="{Binding}" />

当前的DataContext是一个字符串,足以作为一个路径,可以在当前的DataContext中找到一些东西,实际上是不是字符串毕竟?很有道理。

你的意思是这样做,而不是绑定到DataContext?

<Binding />

最新更新