top of page
  • avicoren

Adding context menu to TextBox

Updated: Jan 27, 2022

This post isn't directly related to Material Design, as it is a basic WPF topic, however I thought of showing some basic WPF stuff once in a while...

Context menu (Right Click Menu) can be set on many WPF controls. I will focus on TextBox this time, but I'm sure I will include other controls in future posts.


For this post I will be using one of the early examples we have used. I took the basic example2 files, and created example11, which can be downloaded from my GitHub repo. In this post we will use example11 ps1 and Xaml files as well as the CommonFunctions.ps1 file.

So, in order to add context menu to a TextBox, we will use this syntax:

<TextBox ... >
    <TextBox.ContextMenu>
        <ContextMenu>
            <MenuItem Name="MenuItem1" Header="Do something 1" />
            <MenuItem Name="MenuItem2" Header="Do something 2" />
        </ContextMenu>
    </TextBox.ContextMenu>
</TextBox>

You can optionally use a <separator /> between menu items.


That's how it actually looks like in Xaml:


In the PowerShell code, I've just added a 'add_Click' event for each menu item:


In example11 I've added the classic copy/cut/paste items plus a copy-all item.


Basic. Short. Neat







564 views0 comments

Recent Posts

See All
bottom of page