locked
WPF ContextMenu Placement Issue RRS feed

  • Question

  • I have a DataGrid that I want to show a ContextMenu upon right clicking in a cell of the grid.  I have added the ContextMenu, but I am unable to find a way to get the ContextMenu to appear starting at the mouse point and expand to the right and down.  I have also tried multiple values for ContextMenuService.Placement, but nothing seems to work.  Below is a snippet of my XAML.  I am using .Net 4 and combination of VS2010 and Expression Blend.

    <DataGrid x:Name="gridSelectedReasons" Grid.Row="2" Grid.Column="2" RowHeight="20" FontSize="12" Margin="8,46,8,8" 
    			GridLinesVisibility="Vertical" HeadersVisibility="All"
    			Background="LightGray" RowBackground="LightYellow"
    			AlternatingRowBackground="LightBlue" IsReadOnly="True"
    			AreRowDetailsFrozen="True" AutoGenerateColumns="False"
    			CanUserReorderColumns="False" CanUserResizeColumns="False"
    			CanUserSortColumns="True" FontWeight="Normal" 
                ContextMenuService.HasDropShadow="True" ContextMenuService.Placement="Mouse"
                ItemsSource="{Binding SelectedQuestions}"
    			SelectionChanged="gridSelectedReasons_SelectionChanged"
    			MouseDoubleClick="gridSelectedReasons_MouseDoubleClick" 
                MouseRightButtonUp="gridSelectedReasons_MouseRightButtonUp">
    <DataGrid.ContextMenu>
        <ContextMenu Visibility="Hidden">
            <MenuItem Header="Define Issues" Click="gridSelectedReasons_MenuItem_Click"  />
            <MenuItem Header="Define Scores" Click="gridSelectedReasons_MenuItem_Click" />
        </ContextMenu>
    </DataGrid.ContextMenu>
    <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding ReasonID}" Header="ID" HeaderStyle="{StaticResource DataGridWrappedColumnHeaderStyle}" Width="50" />
            <DataGridTextColumn Binding="{Binding ReasonText}" Header="Reason Text" ElementStyle="{StaticResource DataGridWrappedColumnTextColumnStyle}" Width="*" />
            <DataGridTextColumn Binding="{Binding Languages}" Header="Langs" Width="50" />
        </DataGrid.Columns>
    </DataGrid>
    

     

    Any help or guidance you can provide, would be GREAT.

     


    jim
    Tuesday, July 12, 2011 8:22 PM

Answers

  • Well... That's a shame.
    jim
    • Marked as answer by JRBlack10 Monday, July 18, 2011 2:50 PM
    Monday, July 18, 2011 2:50 PM
  • Hi JRBlack10,

    As for your last concern:

    -->     If there is no easy built in functionality through WPF

    I have done some reaserches on if there is such functionality in WPF, however, the answer is no.

     

    Best regards,


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by JRBlack10 Monday, July 18, 2011 2:50 PM
    Monday, July 18, 2011 8:44 AM

All replies

  • Hi Jim,

     

    It must be one of your styles conflicting with the Context menu because if you have a default datagrid with no style and no style for the context menus then the context menu appears (exactly as you wanted from above).

     

    this line - 

    ContextMenuService.Placement="Mouse"

     

    Actually pushes the context menu a little further down than where the mouse actually is so I would removed it and let WPF use its standard behaviour to control the context menu placement.

     

    Do you think you could post all your styles for the Datagrid and I'll try to replicate the problem on my end.

     

    Thanks. 

     

    Tuesday, July 12, 2011 8:44 PM
  • Below are all the styles from the app.xaml file.  The UserControl where this DataGrid does not have any Styles in it and neither does the MainWindow.xmal.  

    Thanks for taking such a quick look at this.  It has been bugging me for a few hours now.  The first two thirds of these styles are for buttons.  I am new to WPF... maybe 3 months, but I don't see anything in the styles at the bottom for DataGrid(s).

     

    	<Application.Resources>
     
    		<!-- ****************************** -->
    		<!-- WPF Toolkit Extension Styles   -->
    		<!-- ************************************************************************************ -->
    		<Style TargetType="{x:Type extToolkit:MessageBox}">
    			<Setter Property="Background" Value="#FF6ACCFF" />
    			<Setter Property="BorderBrush" Value="Black" />
    			<Setter Property="CaptionForeground" Value="White" />
    			<Setter Property="WindowBorderBrush" Value="Black" />
    			<Setter Property="WindowBackground" Value="#007AC9" />
    			<Setter Property="WindowOpacity" Value="1" />
    			<Setter Property="Foreground" Value="Black"/>
    		</Style>
     
    		<!-- ***************** -->
    		<!-- DataGrid Styles   -->
    		<!-- ************************************************************************************ -->
    		<Style x:Key="DataGridWrappedColumnHeaderStyle" TargetType="{x:Type DataGridColumnHeader}">
    			<Setter Property="ContentTemplate">
    				<Setter.Value>
    					<DataTemplate>
    						<TextBlock TextWrapping="Wrap" Text="{Binding}"></TextBlock>
    					</DataTemplate>
    				</Setter.Value>
    			</Setter>
    		</Style>
    		<Style x:Key="DataGridWrappedColumnTextColumnStyle" TargetType="TextBlock">
    			<Setter Property="TextWrapping" Value="Wrap"/>
    		</Style>
    		<Style x:Key="DataGridWrappedColumnTextColumnTextBoxStyle" TargetType="TextBox">
    			<Setter Property="TextWrapping" Value="Wrap"/>
    			<Setter Property="VerticalScrollBarVisibility" Value="Auto"/>
    			<Setter Property="AcceptsReturn" Value="True"/>
    		</Style>
    		<Style x:Key="DataGridWrappedColumnComboBoxColumnStyle" TargetType="ComboBox">
    			<Setter Property="Height"  Value="20"/>
    			<Setter Property="VerticalAlignment" Value="Top"/>
    		</Style>
     
    		<!-- *****************************  -->
    		<!-- Border Style for Content Area  -->
    		<!-- ************************************************************************************ -->
    		<Style x:Key="ContentAreaBorderStyle" TargetType="Border">
    			<Setter Property="BorderThickness" Value="0" />
    			<Setter Property="Margin" Value="0" />
    			<Setter Property="VerticalAlignment" Value="Stretch" />
    			<Setter Property="HorizontalAlignment" Value="Stretch" />
    			<Setter Property="CornerRadius" Value="10" />
    			<Setter Property="Background" Value="#FF6ACCFF" />
    		</Style>
    		<!-- ***************************  -->
    
    		<!-- Border Style for Popup Area  -->
    		<!-- ************************************************************************************ -->
    		<Style x:Key="PopupAreaBorderStyle" TargetType="Border">
    			<Setter Property="BorderThickness" Value="1" />
    			<Setter Property="Margin" Value="0" />
    			<Setter Property="VerticalAlignment" Value="Stretch" />
    			<Setter Property="HorizontalAlignment" Value="Stretch" />
    			<Setter Property="CornerRadius" Value="10" />
    			<Setter Property="Background" Value="#FFBDBEBD" />
    			<Setter Property="BorderBrush" Value="Black" />
    		</Style>
    		
     
    	</Application.Resources>
    


    jim
    Tuesday, July 12, 2011 8:55 PM
  • Also... I get the same results if I add the same similar ContextMenu to a button on the same UserControl.  Defaults to the left of the mouse instead of the right of the mouse.

    NOTE:  Chrome, AIM, Visual Studio all show ContextMenu to the Right of the mouse.  Office, WORD, Explorer, IE all show to the Left of the mouse.


    jim
    Tuesday, July 12, 2011 9:02 PM
  • Hi Jim,

     

    I couldn't replicate this issue and now I'm a little confused about the whole context menu position.

     

    When I right click for a context menu.

    The left point of the context menu starts exactly where the mouse tip is and then expands to the right.

    Is that not what you want?

     

    Anyhow did you try removing this line -

     

    ContextMenuService.Placement="Mouse"

    Tuesday, July 12, 2011 9:13 PM
  • Yes, I removed that line of code... ="Mouse"... was left in by accident in first posting when I was trying different values.

    I wanted the same results as I get with Visual Studio, AIM, Chrome.... ContextMenu starting with the mouse point and expanding down and to the right.  But what I get is down and to the left... as I now see that it does with all Office applications, IE and Explorer and the desktop.  I am running Windows7.  I always thought these Context menu's went to the right of the mouse....

    So... coworker next to me, running Windows XP and Office 2003, Explorer, IE, etc... the context menu will go to the Right.

    Clearly MS has switched things up quite a bit with Windows 7 and/or Office 2007, because all of those context menu's go to the Left.

    I guess I could just accept it as it is... but I would like the app to work the same regardless of XP or Windows 7.

    Any other ideas?


    jim
    Tuesday, July 12, 2011 9:23 PM
  • Hi,

     

    Unfortunately no. I have windows 7 installed and I don't get this behaviour but maybe it is Office that is somehow affecting the style. The menus on my PC behave the way you want them to.

     

    Hopefully someone else will be able to assist on this issue.

    It is a wieird one. Good luck with it. 

     

    I will keep trying to see a workaround for this. 

    Tuesday, July 12, 2011 9:30 PM
  • Pritesh3,

    Thank you for your responses.  I have found out why the Context Menu is going to the left of the mouse instead of the right for most Microsoft applications (Office, IE, Explorer, etc), but I would still like to find an over-ride ability in WPF.

    The culprit is: HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows\MenuDropAlignment

    If this key is set to 0, then the context menu will show to the left of the mouse pointer.
    If this key is set to 1, then the context menu will show to the right of the mouse pointer.

    My Windows 7 Enterprise install has this key set to 1.  What I don't know is if this is the DEFAULT value for Windows 7 Enterprise, Windows 7 in general or if this key was some how set by some software install or set by our IT department when building the OS image.

     

    NOW... if someone can show me in WPF how to over-ride this key setting and force the context menu to the right (or left for that matter), that would be great!

    I have set this key to 0 and moved on with my life... 


    jim
    Wednesday, July 13, 2011 3:03 PM
  • Hi,

    I dont think I ever would have found that problem!! How did you know where to look?

     

    I guess the only option now is to modify this registry key by C# code?

     

    There is a few articles that will show you how to read registry keys and modify your values.

     

     

    Wednesday, July 13, 2011 3:41 PM
  • Wednesday, July 13, 2011 4:01 PM
  • Hi JRBlack10,

    I think the original issue of your thread has been resolved, as for the last concern:

    -->    if someone can show me in WPF how to over-ride this key setting and force the context menu to the right (or left for that matter), that would be great!

    It will be more relative to C#/VB, instead of WPF, as Preitesh3's suggestion, there are lots of helpful information you could refer to, like:

    http://www.switchonthecode.com/tutorials/csharp-snippet-tutorial-editing-the-windows-registry

    http://social.msdn.microsoft.com/forums/en-us/wpf/thread/880F7376-2C0A-490D-BB0A-6B2EDC1427F1

     

    Best regards,


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, July 14, 2011 8:39 AM
  • Context menu to the left is the default setting for a Windows 7 Tablet PC, the hardware we are writing software for, I assume because it would be easier when using pen on the tablet screen.

    Sure I could change the registry value and be done with this... or write software to change the registry key, (even though this change take effect till after logging out and back into the tablet).

    But that doesn't solve the problem of enabling the developer to decide where to place the context menu... to the right or left of the mouse pointer, regardess of what the registry key says for windows applications... like Chrome, FireFox, Visual Studio 2010, etc...

    I have tried many variations of ContextMenu.Placement and ContextMenu.PlacementRectangle, but nothing seemed to help.  If there is no easy built in functionality through WPF and the .Net Framework, then I will give up this search.

     


    jim
    Thursday, July 14, 2011 12:00 PM
  • Hi JRBlack10,

    As for your last concern:

    -->     If there is no easy built in functionality through WPF

    I have done some reaserches on if there is such functionality in WPF, however, the answer is no.

     

    Best regards,


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by JRBlack10 Monday, July 18, 2011 2:50 PM
    Monday, July 18, 2011 8:44 AM
  • Well... That's a shame.
    jim
    • Marked as answer by JRBlack10 Monday, July 18, 2011 2:50 PM
    Monday, July 18, 2011 2:50 PM