-1

我正在尝试使用 a<MultiBinding>将两个参数传递给我的 ViewModel 的命令,但是在让 XAML 解析器接受我的尝试时遇到问题。

考虑包含在 myUserControl中的以下 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>

所以这就是我想要做的;

  • The context menu has a single item "Send ticket as notification" which, when selected, lists all the available Agentswhich can receive said notification. 这行得通。

  • 当我单击其中一个代理选项时,我希望上下文菜单项同时发送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

感谢您的时间和您可能有的任何建议。

4

1 回答 1

3

这条线很奇怪:

<Binding Path="{Binding}" />

当前的 DataContext 是否是一个足以作为在当前 DataContext 中找到某些东西的路径的字符串,而实际上它毕竟不是一个字符串?完全有道理。

您的意思是这样做而不是绑定到 DataContext 吗?

<Binding />
于 2011-05-03T22:37:12.157 回答