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 4036702
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:15:20+00:00 2026-05-20T12:15:20+00:00

Here is the XAML: <ListBox ItemsSource={Binding Documents} BorderBrush={x:Null}> <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation=Vertical> <TextBlock Text={Binding

  • 0

Here is the XAML:

                <ListBox ItemsSource="{Binding Documents}" BorderBrush="{x:Null}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Vertical">
                            <TextBlock Text="{Binding Title}" FontSize="12" FontWeight="Bold" />
                            <TextBlock Text="{Binding ID}" FontSize="10" FontStyle="Italic" />
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>

                <ListBox.ContextMenu>
                    <ContextMenu ItemsSource="{Binding CategoryList}">
                        <ContextMenu.ItemTemplate>
                            <DataTemplate>
                                <MenuItem Command="{Binding AddDocumentToCategoryContextMenuCommand}" Header="{Binding Category.Name}" />
                            </DataTemplate>
                        </ContextMenu.ItemTemplate>
                    </ContextMenu>
                </ListBox.ContextMenu>

            </ListBox>

Ok so the ListBox’s ItemSource is bound to the Documents collection in the VM and properly renders the Titles and IDs

The Context Menu’s ItemSource is bound to the CategoryList collection in the VM and properly renders the list of categories.

The problem I have is with the Command Binding:

Command="{Binding AddDocumentToCategoryContextMenuCommand}"

Since the ItemSource for the ContextMenu is already set, it tries to get the AddDocumentToCategoryContextMenuCommand from CategoryList. Obviously the command is not there, it is a member of the VM.

I do not want any references to the VMs or Models in the XAML. Everything is constructed using Unity and VM-View is associated in App.xaml:

<Application.Resources>
    <DataTemplate DataType="{x:Type vms:FeedViewModel}">
        <views:FeedView/>
    </DataTemplate>
    <DataTemplate DataType="{x:Type vms:DocumentsViewModel}">
        <views:DocumentsView/>
    </DataTemplate>
    <DataTemplate DataType="{x:Type vms:ManagementViewModel}">
        <views:ManagementView/>
    </DataTemplate>
    <DataTemplate DataType="{x:Type dev:DevelopmentViewModel}">
        <dev:DevelopmentView />
    </DataTemplate>
</Application.Resources>

How can I databind to a member of the VM from within the ContextItem.

Thanks.

UPDATED edit #1 starts Here

Here is the updated xaml (but still not working but some insight gained):

<ListBox ItemsSource="{Binding Documents}" x:Name="Results" BorderBrush="{x:Null}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Vertical">
                            <TextBlock Text="{Binding Title}" FontSize="12" FontWeight="Bold" />
                            <TextBlock Text="{Binding ID}" FontSize="10" FontStyle="Italic" />
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>

                <ListBox.ContextMenu>
                    <ContextMenu ItemsSource="{Binding CategoryList}">
                        <ContextMenu.ItemTemplate>
                            <DataTemplate>
                                <MenuItem Command="{Binding ElementName=Results, Path=DataContext.AddDocumentToCategoryContextMenuCommand}" Header="{Binding Category.Name}" />
                            </DataTemplate>
                        </ContextMenu.ItemTemplate>
                    </ContextMenu>
                </ListBox.ContextMenu>

            </ListBox>

I have this example working for a simple example that does not use a ContextMenu. It appears that the ContextMenu (even though attached to the ListBox) is not part of the user control visual tree. The binding always comes back null / not found. I think the ContextMenu, because it is a floating “window” is constructed in its own tree and therefore cannot find the ListBox call “Results” in order to access the ListBox’s DataContext.

Any thoughts on this? Any recommendations on how deal with?

Edit #2 Starts Here

In case you are are wondering, figured out the answer to the binding question:

This binding works:

Command="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu}, Path=PlacementTarget.DataContext.AddDocumentToCategoryContextMenuCommand}

Hope this helps others with the same question.

One last update for completeness.

In order for the command to know which context menu item was clicked on, I had to change the xaml slightly (silly oversight):

                <ListBox.ContextMenu>
                    <ContextMenu x:Name="Context" ItemsSource="{Binding CategoryList}">
                        <ContextMenu.ItemTemplate>
                            <DataTemplate>
                                <MenuItem Command="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu}, Path=PlacementTarget.DataContext.AddDocumentToCategoryContextMenuCommand}"  
                                            CommandParameter="{Binding Category.ID}"
                                            Header="{Binding Category.Name}" />
                            </DataTemplate>
                        </ContextMenu.ItemTemplate>
                    </ContextMenu>
                </ListBox.ContextMenu>

Again, hope this helps others.

  • 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-20T12:15:21+00:00Added an answer on May 20, 2026 at 12:15 pm

    Use the ElementName property of the binding to specify that. You’d end up with something like this:

    Command="{Binding ElementName=ViewModelObject 
                      Path=AddDocumentToCategoryContextMenuCommand}"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is some XAML <HierarchicalDataTemplate DataType={x:Type data:FolderEntity} ItemsSource={Binding Path=FolderEntities,UpdateSourceTrigger=PropertyChanged}> <Label Content={Binding FolderName}/> </HierarchicalDataTemplate> <TreeView/>
OK I'm redoing this since I messed it up. :) here's the xaml. <Window
I am having trouble with declaratively setting the ItemsSource of a ListBox contained within
Here's a basic regex technique that I've never managed to remember. Let's say I'm
Here's a problem I ran into recently. I have attributes strings of the form
Here is the issue I am having: I have a large query that needs
Here's my scenario - I have an SSIS job that depends on another prior
Here is a simplification of my database: Table: Property Fields: ID, Address Table: Quote
Here is my code, which takes two version identifiers in the form 1, 5,
Here's a coding problem for those that like this kind of thing. Let's see

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.