none
PROBLEM : NOT Able to add Click Event to TreeView Context Menu

    Question

  • In my WPF window, I have a TreeView and added a Context Menu to the TreeView.

    The ContextMenu shows properly, but not able tp fire click event on the menu - here I get error on runtime.

       <TreeView Name="processTreeView" ItemsSource="{Binding ItemProvider}" SelectedItemChanged="processTreeView_SelectedItemChanged" Margin="0,1">
               <TreeView.ItemContainerStyle>
                    <Style TargetType="{x:Type TreeViewItem}">
                        <Setter Property="ContextMenu">
                            <Setter.Value>
                                 <ContextMenu StaysOpen="true">
                                      <MenuItem Header="Import" Name="importCaseMenu" Click="ImportCaseDetails_Click"  />
                                 </ContextMenu>
                            </Setter.Value>
                         </Setter>
                    </Style>
              </TreeView.ItemContainerStyle>
       </TreeView>
    

    With the above code, I get error on run time at Click="ImportCaseDetails_Click" part - XmlParseException - 'Set connectionId threw an exception'.

    // Process Tree - Case - Import
    private void ImportCaseDetails_Click(object sender, RoutedEventArgs e)
     {
          //MessageBox.Show("Import Clicked for : " + processTreeView.SelectedItem.ToString());
    }

    If I remove the Click part, no error. Can figure out Why is this XmlPArseException on Click event code How to overcome this error ?

    Any help is appreciated. Thanks


    Thanks
    If you find any answer helpful, then click "Vote As Helpful" and if it also solves your question then also click "Mark As Answer".

    Thursday, December 12, 2013 11:52 AM

Answers

  • Hi,

    All the events inside the sytle should be given with "EventSetter".

    <TreeView.ItemContainerStyle>
                    <Style TargetType="{x:Type TreeViewItem}">
                        <Setter Property="ContextMenu">
                            <Setter.Value>
                                <ContextMenu StaysOpen="true">
                                    <MenuItem Header="Import" Name="importCaseMenu"   >
                                        <MenuItem.Style>
                                            <Style TargetType="MenuItem">
                                                <EventSetter Event="Click" Handler="ImportCaseDetails_Click"/>
                                            </Style>
                                        </MenuItem.Style>
                                    </MenuItem>
                                </ContextMenu>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </TreeView.ItemContainerStyle>

    • Marked as answer by truptivd Thursday, December 12, 2013 1:27 PM
    Thursday, December 12, 2013 12:30 PM

All replies

  • Hi,

    All the events inside the sytle should be given with "EventSetter".

    <TreeView.ItemContainerStyle>
                    <Style TargetType="{x:Type TreeViewItem}">
                        <Setter Property="ContextMenu">
                            <Setter.Value>
                                <ContextMenu StaysOpen="true">
                                    <MenuItem Header="Import" Name="importCaseMenu"   >
                                        <MenuItem.Style>
                                            <Style TargetType="MenuItem">
                                                <EventSetter Event="Click" Handler="ImportCaseDetails_Click"/>
                                            </Style>
                                        </MenuItem.Style>
                                    </MenuItem>
                                </ContextMenu>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </TreeView.ItemContainerStyle>

    • Marked as answer by truptivd Thursday, December 12, 2013 1:27 PM
    Thursday, December 12, 2013 12:30 PM
  • Thanks, it worked.

    Thanks
    If you find any answer helpful, then click "Vote As Helpful" and if it also solves your question then also click "Mark As Answer".

    Thursday, December 12, 2013 1:27 PM