[英]Can't get selected item context menu
这是我的XAML,我试图在上下文菜单中获取所选项目的值。 我的xaml中没有所选项目的属性。 我是否应该走另一条路线,以便能够从上下文菜单中获取选定的项目?
<Style x:Key="ContextMenuItemStyle" TargetType="{x:Type MenuItem}">
<Setter Property="ItemsSource" Value="{Binding SubItem}"/>
<TextBox Grid.ColumnSpan="8" Grid.RowSpan="2" x:Name="tbRadStudyType" Text="Click to set Care Plan Type" IsReadOnly="True" Margin="2" TextWrapping="Wrap">
<i:Interaction.Triggers>
<i:EventTrigger EventName="PreviewMouseDown">
<i:InvokeCommandAction Command="{Binding PreviewMouseDownCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
<TextBox.ContextMenu>
<ContextMenu ItemsSource="{Binding PlanMainList}"
ItemContainerStyle="{StaticResource ContextMenuItemStyle}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="PreviewMouseDown">
<i:InvokeCommandAction Command="{Binding PreviewMouseDownCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
<ContextMenu.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Term}" />
</DataTemplate>
</ContextMenu.ItemTemplate>
</ContextMenu>
</TextBox.ContextMenu>
</TextBox>
如果我对您的理解正确,那么这是WPF中的常见问题。 这是因为ContextMenu
具有自己的可视化树,与主可视化树完全不同。 这样的结果是它无法从主可视树访问DataContext
。 解决方案是利用Tag
属性传递DataContext
。
与其再次解释整个故事,不如建议您阅读我对datagrid中Add上下文菜单的回答,如何 在ItemsControl中 获取select Item值和Bind Context Menu? 堆栈溢出问题,都在不同情况下解释了同一件事。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.