a を使用<MultiBinding>
して ViewModel のコマンドに 2 つのパラメーターを渡そうとしていますが、XAML パーサーが私の試行を受け入れるのに問題があります。
オブジェクトUserControl
のコレクションにバインドされているmy に含まれる次の 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>
これが私がやろうとしていることです。
コンテキスト メニューには「チケットを通知として送信」という 1 つの項目があり、これを選択すると、
Agents
通知を受け取ることができる利用可能なすべての項目が一覧表示されます。これは機能します。これらのエージェント オプションの 1 つをクリックすると、コンテキスト メニュー項目から、コンテキスト メニューを表示するため
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>
コンテキストメニューの実際のセットアップは、私には複雑に思えますが。パラメーターの 1 つがMultiBinding
クリックされたアイテムにバインドされた実際のアイテムであるという実際の指定は、ContextMenu.MenuItem
非常に単純なコマンドのようです。コンテキスト メニューにはすべてのエージェントが正しく一覧表示されるので、この現在のオブジェクトをコマンド パラメーターとして送信するように要求するだけでよいと思います。私も試しました。
<Binding RelativeSource={RelativeSource AncestorType={x:Type MenuItem}} Path="SelectedItem" />
としても
<Binding RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}} Path="SelectedItem" />
しかし、パラメーター コンバーターに送信されるのは、UnsetValue
お時間を割いていただき、ありがとうございました。