Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Greetings All,
I am getting some weird behavior from WPF when i create an event to programatically open a context menu. once I select a text and right click the highlight of the selection disappears once the context menu opens up.
Here is a sample of the problem:

Xaml:


XML
<Window x:Class="WpfApplication19.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="287" Width="419">
    <Grid>
        <TextBox x:Name="myText" Height="38" Margin="0,72,6,0" VerticalAlignment="Top" HorizontalAlignment="Right" Width="135"></TextBox>
        <Label Height="30" Margin="12,80,164,0" Name="label1" VerticalAlignment="Top">Textbox with contextMenu property set</Label>
        <Label Height="30" Margin="12,0,136,91" Name="label2" VerticalAlignment="Bottom">TextBox Open ContextMenu by programmatically</Label>
        <TextBox Height="38" Margin="0,0,6,81" x:Name="myText1" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="135" />
    </Grid>
</Window>


And Code Behind:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication19
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        ContextMenu con = new ContextMenu();
        public Window1()
        {
            InitializeComponent();
            MenuItem menuItem1 = new MenuItem();
            menuItem1.Header = "Menu1";
            MenuItem menuItem2 = new MenuItem();
            menuItem2.Header = "Menu2";

            con.Items.Add(menuItem1);
            con.Items.Add(menuItem2);

            this.myText.ContextMenu = con;

            this.myText1.PreviewMouseDown += new MouseButtonEventHandler(myText1_PreviewMouseDown);

        }




        void myText1_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            base.OnPreviewMouseDown(e);
            if (e.RightButton == MouseButtonState.Pressed)
            {
               con.Placement = System.Windows.Controls.Primitives.PlacementMode.MousePoint;

                con.IsOpen = true;
                IInputElement focusedElement = FocusManager.GetFocusedElement(this);


            }
        }



    }
}



Thank You in advance!
Posted

1 solution

There is no HideSelection property for WPF controls but you can have a look at these links. Maybe that could work out for you.
http://social.msdn.microsoft.com/Forums/en/wpf/thread/0f0858ed-eb14-4b3d-8d39-88034bc096f4[^]

http://social.msdn.microsoft.com/forums/en-US/wpf/thread/ac968cc6-d71d-4710-affd-8fec14085017/[^]

Good luck!
 
Share this answer
 
Comments
nervous_cat 15-Feb-11 6:36am    
Hey NijBoer,

Thx for the answer! but it doesnt quite help me out, since e.handled = true denies me access to other text boxes.

I did try setting contextmenu.focusable to false, which works perfectly, but am unsure why that works.

It is not a solution as long as I find a good reason to use it.
E.F. Nijboer 15-Feb-11 7:34am    
Sorry that it doesn't quite help you out but there isn't a way to deal with it easily. Maybe writing a custom textbox could work out but would be somewhat more work.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900