在 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>
这就是我正在尝试做的事情;
上下文菜单中有一个项目“发送工单作为通知”,选择该项目后,会列出可以接收所述通知的所有可用
代理
。这有效。当我单击这些代理选项之一时,我希望上下文菜单项发送从
listview
单击的ticket
项以显示上下文菜单(有效)以及我从上下文菜单中选择的Agent
项。我已经通过MultiBinding
实现了一半目标<绑定RelativeSource =“{RelativeSource AncestorType = {x:Type ListView}}”Path =“SelectedItem”/> > <绑定路径="{绑定}"/>
,尽管上下文菜单的实际设置对我来说似乎有些复杂。 MultiBinding 参数之一应该是绑定到单击的 ContextMenu.MenuItem 的实际项目的实际规范似乎是一个非常简单的命令。上下文菜单正确列出了所有代理,因此我认为只需要求将当前对象作为命令参数发送即可,很简单。我也尝试过;
<Binding RelativeSource={RelativeSource AncestorType={x:Type MenuItem}} Path="SelectedItem" />
以及 但是
<Binding RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}} Path="SelectedItem" />
发送到参数转换器的所有内容都是 UnsetValue
感谢您的宝贵时间以及您可能提出的任何建议。
I am attempting to use a <MultiBinding>
to pass two parameters to my ViewModel's command but am having issues getting the XAML parser to accept my attempts.
Consider the following ListView contained within my UserControl
which is bound to a collection of Ticket
objects.
<ListView x:Name="lvTicketSummaries"
ItemsSource="{Binding TicketSummaries}"
ItemContainerStyle="{DynamicResource ResourceKey=ListViewItem}"
IsSynchronizedWithCurrentItem="True">
The respective style ListViewItem
<Style x:Key="ListViewItem" TargetType="{x:Type ListViewItem}">
<Setter Property="ContextMenu" Value="{DynamicResource ResourceKey=cmListViewItem}"/>
<!-- A load of irrelevant stuff ->
</Style>
And the referenced ContextMenu
item where the source of my query sits;
<!-- 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>
So here is what I am attempting to do;
The context menu has a single item "Send ticket as notification" which, when selected, lists all the available
Agents
which can receive said notification. This works.When I click on one of these agent options, I want the context menu item to send both the
ticket
item that was clicked on from thelistview
to show the context menu (this works) AND theAgent
item I selected from the context menu. I have half-achieved this with theMultiBinding
<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>
Now although the actual set-up of the context menu seems some what complicated to me. The actual specification that one of the MultiBinding
parameters should be the actual item bound to the clicked ContextMenu.MenuItem
seems quite a simple command. The context menu correctly lists all Agents and so I would of thought that simply asking for this current object to be sent through as a command parameter, simple. I have also tried;
<Binding RelativeSource={RelativeSource AncestorType={x:Type MenuItem}} Path="SelectedItem" />
as well as
<Binding RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}} Path="SelectedItem" />
But all that gets sent to the Parameter converter is UnsetValue
Thank you for your time and any suggestions you might have.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这行代码很奇怪:
当前的 DataContext 是否是一个字符串,足以作为在当前 DataContext 中找到某些内容的路径,而实际上不是字符串?很有道理。
您的意思是这样做而不是绑定到 DataContext 吗?
This line is odd:
Is the current DataContext a string that would suffice as a path that would find something in the current DataContext which is in fact not a string after all? Makes perfect sense.
Did you mean to do this instead which binds to the DataContext?