Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7079717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:42:23+00:00 2026-05-28T06:42:23+00:00

I am trying to bind a context menu item to a command that I

  • 0

I am trying to bind a context menu item to a command that I have defined in my ViewModel. The context menu sits inside a ListView that I also have bound to a CollectionViewSource, and this I think is the cause of the problem.

I have managed to bind the selected item in the listView collection to my ViewModel, but when I am trying to use the same way to bind the context menu item command to my ViewModel it doesn’t work. I hope that anybody have the time to read through all the code below and give me some idea about what I am doing wrong.

Ps. I have had to change some of the names in order to not give away what the application is about.

In my ViewModel I have defined the followning:

public ObservableCollection<ListItemViewModel> ListViewItemViewModels {get; set;}

public MyListItem SelectedListItemViewModel {get; set;}

private RelayCommand _runCommand;
public ICommand RunCommand {
  get {
     return _runCommand ??
       ( _runCommand = new RelayCommand( param => RunReport(), param => CanRunReport ) );
  }
}

private void RunReport() {
    Logger.Debug("Run report");
}

Then in my View I have set upp a ListView as follows:

<ListView DataContext="{StaticResource ListGroups}"
   ItemsSource="{Binding}" 
   ItemContainerStyle="{StaticResource ListItemStyle}" 
   IsSynchronizedWithCurrentItem="True" 
   SelectedItem="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Grid},AncestorLevel=1 }, Path=DataContext.SelectedListItem, UpdateSourceTrigger=PropertyChanged}" 
   Margin="10,10,0,10">
   <ListView.GroupStyle>
        <StaticResourceExtension ResourceKey="AccountGroupStyle"/>
   </ListView.GroupStyle>
   <ListView.View>
       <GridView>
            <GridViewColumn Header="Title" DisplayMemberBinding="{Binding Path=DisplayTitle}"/>
            <GridViewColumn Header="Date" DisplayMemberBinding="{Binding Path=DateString}"/>
        </GridView>
   </ListView.View>
   <ListView.ContextMenu>
       <ContextMenu Name="ListViewContextMenu">
          <MenuItem Header="Run" Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Grid},AncestorLevel=1 }, Path=DataContext.RunCommand}"/>
       </ContextMenu>
   </ListView.ContextMenu>


</ListView>

The CollectionViewSource is defined as follows:

<DataTemplate x:Key="ListViewListTemplate" DataType="{x:Type ViewModels:ListItemViewModel}">
    <StackPanel Orientation="Vertical">
        <TextBlock Text="{Binding Path=DisplayTitle}" Margin="8,0,0,0"/>
    </StackPanel>
</DataTemplate>

<CollectionViewSource Source="{Binding Path=ListItemViewModels}" x:Key="ListItemGroups">
    <CollectionViewSource.GroupDescriptions>
        <PropertyGroupDescription PropertyName="ListItemGroupName"/>
    </CollectionViewSource.GroupDescriptions>
    <CollectionViewSource.SortDescriptions>
        <ComponentModel:SortDescription PropertyName="Index" Direction="Ascending"/>
        <ComponentModel:SortDescription PropertyName="DisplayTitle" Direction="Ascending"/>
    </CollectionViewSource.SortDescriptions>
</CollectionViewSource>

<GroupStyle x:Key="ListItemGroupStyle">
    <GroupStyle.HeaderTemplate>
        <DataTemplate>
            <!-- The text binding here is refered to the property name set above in the propertyGroupDescrition -->
            <TextBlock x:Name="text" Background="{StaticResource DateGroup_Background}" FontWeight="Bold" Text="{Binding Path=Name}"
                      Foreground="White"
                     Margin="1"
                     Padding="4,2,0,2"/>

        </DataTemplate>
    </GroupStyle.HeaderTemplate>
</GroupStyle>

<Style x:Key="ListItemStyle" TargetType="{x:Type ListViewItem}">

    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>

    <!-- 
  Bind the IsSelected property of a ListViewItem to the 
  IsSelected property of a ReconciliationTaskViewModel object.
  -->
    <Setter Property="IsSelected" Value="{Binding Path=IsSelected, Mode=TwoWay}" />
    <Style.Triggers>
        <MultiTrigger>
            <MultiTrigger.Conditions>
                <Condition Property="ItemsControl.AlternationIndex" Value="1" />
                <Condition Property="IsSelected" Value="False" />
                <Condition Property="IsMouseOver" Value="False" />
            </MultiTrigger.Conditions>
            <Setter Property="Background" Value="#EEEEEEEE" />
        </MultiTrigger>
    </Style.Triggers>
</Style>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T06:42:23+00:00Added an answer on May 28, 2026 at 6:42 am

    The cause of the problem is that the ContextMenu isn’t part of either the logical or visual tree of the ListView, so RelativeSource/FindAncestor doesn’t work, and the DataContext is not inherited.

    I posted a solution to this problem a few months ago, you can use it as follows:

    <ListView ...>
        <ListView.Resources>
            <local:BindingProxy x:Key="proxy" Data="{Binding}" />
        </ListView.Resources>
    
        ...
    
       <ListView.ContextMenu>
           <ContextMenu Name="ListViewContextMenu">
              <MenuItem Header="Run" Command="{Binding Source={StaticResource proxy}, Path=Data.RunCommand}"/>
           </ContextMenu>
       </ListView.ContextMenu>
    
    
        ...
    
    </ListView>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ListActivity that I'm trying to bind to an array of business
I have a pretty simple local service that I'm trying to bind to my
Im trying to bind a list with datetime objects to my repeater. if (e.Item.ItemType
I'm trying to bind a XAML ComboBox so that its list items are the
I'm trying to bind to a property of a container from inside a DataTemplate.
I'm trying to make changing background image by menu item. It's works now but
I have a WPF application that I'm trying to dynamically add items to a
I am trying to create a menu bar for my application. I have created
I have my shared user defaults plist that looks something like: menuItems (Array) Item
I have a ContextMenu defined on a Datagrid but want to bind submenu items

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.