Frage

I have the "simple" task to have a ContextMenu on a TreeView(Element) that is done in MVVM-way. When searching the web I found some solutions that I could bring to work with buttons etc. but not with the TreeView. I think the problem is with setting the ItemsSource-Property of TreeView that gives every single item an own DataContext.

Here's my little Test-App where you can see the principle working for button but not for the TreeView-Elements:

MainWindow.xaml:

<Window x:Class="ContextMenu.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">

    <Grid >
        <StackPanel>
            <TextBlock Text="{Binding MyText}" />

            <Button Tag="{Binding DataContext,RelativeSource={RelativeSource Mode=Self}}" Content="Click me">
                <Button.ContextMenu>
                    <ContextMenu>
                        <MenuItem Header="{Binding PlacementTarget.Tag.MyText,
                            RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ContextMenu}}" />
                    </ContextMenu>
                </Button.ContextMenu>
            </Button>

            <TreeView ItemsSource="{Binding MyList}" Tag="{Binding DataContext, RelativeSource={RelativeSource Mode=Self}}">
                <TreeView.ItemTemplate>
                    <HierarchicalDataTemplate>
                        <TextBlock Text="{Binding Name}">
                            <TextBlock.ContextMenu>
                                <ContextMenu>
                                    <MenuItem Header="{Binding Path=PlacementTarget.Tag.MyText, 
                                        RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ContextMenu}}" />
                                </ContextMenu>
                            </TextBlock.ContextMenu>
                        </TextBlock>
                    </HierarchicalDataTemplate>
                </TreeView.ItemTemplate>         
            </TreeView>

        </StackPanel>        
    </Grid>
</Window>

Codebehind:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = new MainWindowVM();
    }
}

MainWindowVM.cs:

public class MainWindowVM
{
    public string MyText { get; set; }
    public ObservableCollection<TreeElement> MyList { get; set; }

    public MainWindowVM()
    {
        MyText = "This is my Text!";
        MyList = new ObservableCollection<TreeElement>();
        MyList.Add(new TreeElement("String 1"));
        MyList.Add(new TreeElement("String 2"));
    }
}

public class TreeElement
{
    public string Name { get; set; }
    public TreeElement(string Name)
    {
        this.Name = Name;
    }
}

Thanks for your help!! Joerg

War es hilfreich?

Lösung

You are close.

What you are doing:

  1. you set the Tag of TreeView to its own DataContext. This is unnecessary.
  2. you try to get Tag.MyText from your ContextMenu.PlacementTarget - which is TextBlock. The TextBlock has no Tag set.

What you should do:

  1. set the Tag of the TextBlock to DataContext of the Window (Window is TextBlock ancestor and you should look it up via RelativeSource Mode=FindAncestor).
  2. the second part is OK - you have TextBlock.Tag set in the first step.
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top