1

i have a ContextMenu on a Button in XAML. The ContextMenu Items are loaded by the ItemsSource property. Now I want to set the text of the button with the SelectedItem.Text of the ContextMenu. Is there any solution to do this?

Here is a snippet of my XAML code:

<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>
user2025830
  • 872
  • 2
  • 17
  • 37
  • i've tried this in the content property of the button: Content={Binding RelativeSource={RelativeSource Mode=Self}, Path=Items[0].SelectedItem.Text} – user2025830 Jan 30 '13 at 15:11
  • 1
    Your XAML for the `ItemsControl` probably doesn't work. `ContextMenus` are not part of the same VisualTree as the rest of the application, so bindings don't work like you expect them to. You can use the `PlacementTarget` of the `ContextMenu` to get a reference to the main VisualTree, like [this](http://stackoverflow.com/a/5236744/302677) – Rachel Jan 30 '13 at 16:31
  • can anyone give me an example for my situation, because i don't understand how i can bin the selected item from the context menu to the text of the button – user2025830 Jan 31 '13 at 08:06

0 Answers0