我正在尝试使用 a<MultiBinding>
将两个参数传递给我的 ViewModel 的命令,但是在让 XAML 解析器接受我的尝试时遇到问题。
考虑包含在 myUserControl
中的以下 ListView,它绑定到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>
所以这就是我想要做的;
The context menu has a single item "Send ticket as notification" which, when selected, lists all the available
Agents
which can receive said notification. 这行得通。当我单击其中一个代理选项时,我希望上下文菜单项同时发送
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
似乎是一个非常简单的命令。上下文菜单正确列出了所有代理,因此我认为只需要求将此当前对象作为命令参数发送,很简单。我也试过;
<Binding RelativeSource={RelativeSource AncestorType={x:Type MenuItem}} Path="SelectedItem" />
也
<Binding RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}} Path="SelectedItem" />
但是所有发送到参数转换器的是UnsetValue
感谢您的时间和您可能有的任何建议。