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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:06:19+00:00 2026-06-13T22:06:19+00:00

I have a ContextMenu defined in XAML and I modify it in code: ContextMenu

  • 0

I have a ContextMenu defined in XAML and I modify it in code:

ContextMenu EditContextMenu;
EditContextMenu  = (ContextMenu)this.FindResource("EditContextMenu");
//Modify it here...

Then, I need to set it as a ContextMenu for all TextBoxes, DatePickers, etc in a XAML theme file using data binding. I’ve tried adding a property to the main window :

    public ContextMenu sosEditContextMenu
    {
        get
        {
            return EditContextMenu;
        }
    }

…and binding it like this (the folowing is from a theme file with ‘FTWin‘ being the Name of my main window where the sosEditContextMenu property is defined):

<Style TargetType="{x:Type TextBox}">
    <Setter Property="ContextMenu" Value="{Binding Source=FTWin, Path=sosEditContextMenu}"/>
</Style>

…but it doesn’t work. I’ve tried various things and I either got Exceptions about resources not being found or nothing happened.

Is what I’m trying to do possible and, if yes, what am I doing wrong?
I don’t know if setting the DataContext of an object could help, but setting it for all TextBoxes by code is not so nice right?

  • 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-06-13T22:06:20+00:00Added an answer on June 13, 2026 at 10:06 pm

    Put the menu that you defined in xaml in a resource dictionary that can be seen from the textbox and instead of using a binding just use StaticResource to link it in your style.

    <Window x:Class="ContextMenu.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525">
    
        <Window.Resources>
    
            <!-- The XAML defined context menu, note the x:Key -->
            <ContextMenu x:Key="EditContextMenu">
                <ContextMenu.Items>
                    <MenuItem Header="test"/>
                </ContextMenu.Items>
            </ContextMenu>
    
            <!-- This sets the context menu on all text boxes for this window .-->
            <Style TargetType="{x:Type TextBox}">
                <Setter Property="ContextMenu" Value="{StaticResource EditContextMenu}"/>
            </Style>        
        </Window.Resources>
    
        <Grid>
    
            <!-- no context menu needs to be defined here, it's in the sytle.-->
            <TextBox />
        </Grid>
    </Window>
    

    You can still change it in code behind by looking for the resource

    public MainWindow()
    {
        InitializeComponent();
    
        System.Windows.Controls.ContextMenu editContextMenu = (System.Windows.Controls.ContextMenu)FindResource("EditContextMenu");
        editContextMenu.Items.Add(new MenuItem() { Header = "new item" });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a WPF ListView control with a ContextMenu written in XAML Code. With
I have this problems where I have all these CommandBindings defined in the MainWindow,
I have an image that has a nested context menu defined in the XAML
I have a contextmenu that shows on certain elements when you right click. This
I have a SubMenu in a ContextMenu which ItemSource is set to a expression
I have a XAML based ContextMenu bound to the rows in a datagrid. It
I have a tricky problem where I am binding a ContextMenu to a set
I have following ContextMenu in my xaml: <ContextMenu ItemsSource={Binding RSPContextMenuCommands}> <ContextMenu.ItemContainerStyle> <Style TargetType={x:Type MenuItem}>
I have a ContextMenu defined on a Datagrid but want to bind submenu items
I have styled my Menu/ContextMenu/MenuItem controls in App.xaml so that those styles apply to

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.