我正在尝试使用<MultiBinding>
将两个参数传递给我的ViewModel命令,但是在让XAML解析器接受我的尝试时遇到了问题。
考虑我的UserControl
中包含的以下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>
所以这就是我试图做的事情;
上下文菜单中有一个项目“作为通知发送票证”,选中后会列出可以接收所述通知的所有可用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
的实际项目的实际规范似乎是一个非常简单的命令。上下文菜单正确列出了所有代理,因此我认为只需要将当前对象作为命令参数发送,简单。我也试过了;
<Binding RelativeSource={RelativeSource AncestorType={x:Type MenuItem}} Path="SelectedItem" />
以及
<Binding RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}} Path="SelectedItem" />
但是所有发送到参数转换器的都是UnsetValue
感谢您的时间和任何建议。
答案 0 :(得分:3)
这条线很奇怪:
<Binding Path="{Binding}" />
当前的DataContext是否足以作为在当前DataContext中找到某些东西的路径,实际上不是字符串?有一个完美的感觉。
你的意思是这样做而不是绑定到DataContext吗?
<Binding />