Modify Treeview Context Menu Header RRS feed

  • Question

  • Hi 
    Thank you for your help on my previous threads. 

    Here is my Treeview with the context menu. 

     <TreeView ItemsSource="{Binding Path=allFeeds,ElementName=Reader}"
                  x:Name="tviFeeds"   Margin="11,47,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" Width="289" Height="528" Background="#FFE4E4E4">
                    <ContextMenu x:Key ="TagContext"  StaysOpen="true" x:Shared="False">
                        <MenuItem Header="All marked as unread" Name="tviOption1" Click="markasreadClick"  IsCheckable="True" IsChecked="True"></MenuItem>
    					<MenuItem Header="Edit Subscription" Name="tviOption2" ></MenuItem>
    					<MenuItem Header="Unsubscribe" Name="tviOption3" ></MenuItem>
                    <HierarchicalDataTemplate ItemsSource="{Binding Path=detailsFeeds}">
                        <TextBlock Text="{Binding Path=Title}" />

    I can change the header when i click on menuItem but not change if click is from another control .

    the first menuItem is "All marked as unread" and it is checked. 
    On   TreeViewItem click, a datagrid will be filled. 
    once a row is selected in the datagrid, I want to change the 
    "All marked as unread" ==> "Mark all as read" in the context menu of the Treeview. 
    And also uncheck the Menu Item.

    Thanks again for your help.

    Wednesday, July 2, 2014 11:36 AM


  • Your code doesn't make much sense. How do for example apply the "TagContext" ContextMenu to the TreeViewItems? Please don't expect anyone to be able to guess how your code look likes and behaves without having seen it. It makes it hard to help you if don't post all relevant parts of your code.

    If you share the same ContextMenu among all TreeViewItems (x:Shared="True"), you can just change the properties of the first MenuItem by accessing the resource in code:

    ContextMenu contextMenu = tviFeeds.Resources["TagContext"] as ContextMenu;
    MenuItem mi = contextMenu.Items[0] as MenuItem;
    mi.Header = "some text....";
    mi.IsSelected = false;

    If you don't, you could bind to some property that decides which property value to set. You could for example define a string property in your view model or in the window class that returns the string to be displayed for all ContextMenus and bind the Header property of the MenuItem in the ContextMenu to this one. 

    You could also bind it to a property on your Feed class or whatever you call it and bind it to this one. Then you just set this property for all items in the ItemsSource when you want to change the text.

    Wednesday, July 2, 2014 6:04 PM