How to get the contextmune header on check of context menu icon as radio button checked in WPF C#? RRS feed

  • Question

  • I am creating context menu programatically in c# wpf, My context menu items are installed driver in my system. and contextmenu icon are radio button, when I am clicking on contextmenu headre I can able to enable corresponding radio button, like

    RadioButton rb = menuItem.Icon as RadioButton;
               if (rb != null)
                   rb.IsChecked = true;}

    its working fine, but If I checked radio button i want corresponding contextmenu header name how can I do this.?

    I tried to 
    radBtn.Checked += radBtn_Checked;
    void radBtn_Checked(object sender, RoutedEventArgs e)

    Tuesday, November 7, 2017 4:46 AM


  • Check this:

    RadioButton rb = (RadioButton)e.Source;
    DependencyObject o = rb;
    MenuItem item = null;
       o = VisualTreeHelper.GetParent( o );
       if( o == null ) break;
       item = o as MenuItem;
    } while( item == null );
    string header = item?.Header as string;

    • Edited by Viorel_MVP Tuesday, November 7, 2017 6:13 AM
    • Marked as answer by sushilbhat Tuesday, November 7, 2017 6:54 AM
    Tuesday, November 7, 2017 6:11 AM