WPF绑定从ContextMenu选择EdItem到Button.Content
我在XAML中的按钮上有一个上下文菜单。ContextMenu项由ItemsSource属性加载。现在我想用ContextMenu的SelectedItem.text设置按钮的文本。有什么解决办法吗 以下是我的XAML代码片段:WPF绑定从ContextMenu选择EdItem到Button.Content,wpf,xaml,button,binding,contextmenu,Wpf,Xaml,Button,Binding,Contextmenu,我在XAML中的按钮上有一个上下文菜单。ContextMenu项由ItemsSource属性加载。现在我想用ContextMenu的SelectedItem.text设置按钮的文本。有什么解决办法吗 以下是我的XAML代码片段: <Button Grid.Column="4" Grid.Row="1" Width="100" Height="24" BorderThickness="0" Command="{Binding SwitchFieldClickCommand}" Content
<Button Grid.Column="4" Grid.Row="1" Width="100" Height="24" BorderThickness="0" Command="{Binding SwitchFieldClickCommand}" Content="{}">
<Button.ContextMenu>
<ContextMenu Name="CM_SwitchField">
<ItemsControl ItemsSource="{Binding Benutzer.SwitchFieldList}" />
</ContextMenu>
</Button.ContextMenu>
</Button>
我在按钮的content属性中尝试过:content={Binding RelativeSource={RelativeSource Mode=Self},Path=Items[0]。SelectedItem.Text}Items控件的XAML可能不起作用。上下文菜单与应用程序的其余部分不属于同一个VisualTree,因此绑定的工作方式与您期望的不同。您可以使用ContextMenu的PlacementTarget来获取对主VisualTree的引用,就像任何人都可以给我举一个例子说明我的情况一样,因为我不明白如何将所选项目从上下文菜单放入按钮的文本中