【问题标题】:Getting ContextMenu target from Item in ItemsControl从 ItemsControl 中的 Item 获取 ContextMenu 目标
【发布时间】:2016-02-17 23:09:51
【问题描述】:

我有一个 ItemsControl 绑定到我的 ViewModel 上的一个集合。这个 ItemsControl 提供了几个“消息”。我需要一个 ContextMenu,当单击它时,它会提供一个选项来将该特定消息复制到剪贴板。

Command 应该在 ViewModel 上激活,而我要传递的 CommandParameter 是 Message 本身。

我遇到的问题是获得 ContextMenu 已打开 OVER 的实际消息。

我尝试了多种不同的方法,但似乎无法找到将消息作为命令参数传递的方法。

我应该寻找一种不同的方法来实现这一点吗?将 ItemsControl 与 ItemsPresenter 一起使用是否存在问题?

<ScrollViewer CanContentScroll="False" 
            VerticalScrollBarVisibility="Auto" 
            HorizontalScrollBarVisibility="Disabled" 
            Grid.RowSpan="1">
            <ItemsControl ItemsSource="{Binding MyActiveConversation.OrderedMessages, UpdateSourceTrigger=PropertyChanged}" 
            IsEnabled="{Binding MyActiveConversation.IsOptOut, Converter={StaticResource BoolToEnabledInverter}}"
            ItemTemplateSelector="{StaticResource tSelector}" 
            VirtualizingPanel.IsVirtualizing="False" 
            SourceUpdated="SourceUpdatedHandler" MinWidth="450">
                <ItemsControl.ContextMenu>
                    <ContextMenu>
                        <MenuItem Header="Copy" Template="{DynamicResource MenuItemControlTemplate}" 
                            CommandParameter="{Binding}" Command="{Binding Path=Data.CopyTextMessageCommand, Source={StaticResource ContextProxy}}">
                        </MenuItem>
                    </ContextMenu>
                </ItemsControl.ContextMenu>
                <ItemsControl.Template>
                    <ControlTemplate>
                        <Grid>
                            <ItemsPresenter VirtualizingPanel.IsVirtualizing="False"/>
                        </Grid>
                    </ControlTemplate>
                </ItemsControl.Template>
            </ItemsControl>

【问题讨论】:

    标签: c# wpf mvvm


    【解决方案1】:

    CommandParameter="{Binding}" 对我有用,CommandParameter="{Binding .}" 也一样

    【讨论】:

    • 我编辑了我的问题以包含 XAML。如果我对 CommandParameter 使用 {Binding},它会在后台返回 ViewModel。我正在使用代理来检索 DataContext。
    猜你喜欢
    • 2017-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-11
    • 1970-01-01
    • 2012-07-25
    • 1970-01-01
    相关资源
    最近更新 更多