【发布时间】:2012-12-27 18:25:41
【问题描述】:
我有一个 ListView,其中包含来自字符串列表的 ItemSource。 现在我添加了一个上下文菜单,它应该只实现一些命令。但问题在于如何设置 DataContext。找到了一些解决方案,但没有一个对我有用。不知道我的错在哪里。
这是我的 XAML 代码,已简化为重要区域。
<ListView x:Name="lstBackups" ItemsSource="{Binding SelectedClient.Backups}">
<ListView.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu>
<MenuItem Header="Do Something" DataContext="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu}}"
cinch:SingleEventCommand.RoutedEventName="MouseLeftButtonUp"
cinch:SingleEventCommand.TheCommandToRun="{Binding Path=DataContext.OpenBackupInExplorerCommand, ElementName=UserControl}">
</MenuItem>
</ContextMenu>
</Setter.Value>
</Setter>
</Style>
</ListView.ItemContainerStyle>
</ListView>
【问题讨论】:
-
如果您不发布您已尝试过的帖子,将很难为您提供帮助。贴出你的viewModel代码,你可以尝试使用snoop查看绑定错误
标签: c# xaml mvvm datacontext menuitem