【发布时间】:2014-02-19 21:28:24
【问题描述】:
这是我的 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>
【问题讨论】:
-
ContextMenus 与其他 UI 控件不属于同一 VisualTree。如果您想使用绑定将数据拉入您的 ContextMenu,您需要使用
PlacementTarget属性找到 ContextMenu 附加到的控件,然后从那里开始。有关示例,请参见 here。