Click here to Skip to main content
15,918,003 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
After two days of googling and searching I give it up. Seems to me it is becoming big challenge :)

So I want to set two different context menus based on xml element name.

Lets say for group Add group and for document Add document

Prefering to do it in xaml without behind code.

One context menu should be when group element is right clicked other one when document element is clicked.

I have fairly simple xml structure:
XML
<?xml version="1.0" encoding="utf-8"?>
<docspacket>
  <packet>
    <group>
      <name>Home group</name>
        <group>
          <name>Other group</name>
          <document>
            <name>doc1</name>
          </document>
          <document>
            <name>doc2</name>
          </document>
        </group>
      <document>
        <name>doc1</name>
      </document>
      <document>
        <name>doc2</name>
      </document>
    </group>
  </packet>
</docspacket>


So my treeView have HierachicalDataView:

XML
<Grid.Resources>
            <HierarchicalDataTemplate x:Key="Group" ItemsSource="{Binding XPath=./*[name]}">
                <TextBlock Text="{Binding XPath=name}" />
            </HierarchicalDataTemplate>
            <XmlDataProvider x:Key="XmlNodeList" XPath="/docspacket/packet/*" Source="C:\Users\home\Documents\Visual Studio 2010\Projects\SimpleXMLparser\SimpleXMLparser\bin\Debug\XMLFile2.xml"/>
        </Grid.Resources>


And treeView control:

<TreeView ItemTemplate="{StaticResource Group}" ItemsSource="{Binding Source={StaticResource XmlNodeList}}" Height="229" HorizontalAlignment="Left" Margin="254,41,0,0" Name="treeView1" VerticalAlignment="Top" Width="404" SelectedItemChanged="treeView1_SelectedItemChanged" />


So maybe someone knows how should I implement such functionality?
Posted

1 solution

The best approach would be using only one single Context Menu instance per Tree View control (or even a set of controls). The set of items if their states (IsEnabled, IsVisible, etc.) should be modified in the handle of the event System.Windows.Controls.ContextMenu.ContextMenuOpening (at the moment of processing the menu is not yet visible which is important). I would also prefer changing menu items property over re-populating of the items — it would give you a bit better performance, but more importantly would help to avoid some bugs.

I understand, you menu should look different for different Tree View Node. There is another trick: put all data relevant to the preparation of the Context Menu in the handler of System.Windows.Controls.ContextMenu.ContextMenuOpening. It should be some reference type as this data can be shared among many items.

—SA
 
Share this answer
 
Comments
evaldas86 28-Apr-11 6:11am    
Following your suggestion I used ContextMenuOpening event and used ContextMenu resource to set TextBlock ContextMenu in event handler.
Sergey Alexandrovich Kryukov 28-Apr-11 12:02pm    
Great. Hope it works for you.
Thanks for accepting this Solution.
Good luck,
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900