【发布时间】:2011-04-09 09:12:37
【问题描述】:
我正在尝试使用 <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。这行得通。-
当我单击这些代理选项之一时,我希望上下文菜单项同时发送从
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
感谢您的宝贵时间以及您可能提出的任何建议。
【问题讨论】:
标签: wpf xaml data-binding contextmenu multibinding