我試圖使用<MultiBinding>
將兩個參數傳遞給ViewModel的命令,但遇到問題讓XAML解析器接受我的嘗試。在MultiBinding命令參數中包含綁定對象
請考慮以下列表視圖,該視圖包含在我的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
可以接收所述通知。這工作。當我點擊這些代理選一個,我想上下文菜單項,同時發送的
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
的實際項目,這似乎是一個非常簡單的命令。上下文菜單正確地列出了所有Agent,所以我認爲只需要簡單地要求將當前對象作爲命令參數發送。我也嘗試過;
<Binding RelativeSource={RelativeSource AncestorType={x:Type MenuItem}} Path="SelectedItem" />
以及
<Binding RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}} Path="SelectedItem" />
但所有的被髮送到參數轉換爲UnsetValue
謝謝你的時間,你可能有任何建議。