18

I'm coming from WinForms and teaching myself WPF for the first time.
I thought I'd start by recreating the interface of one of my WinForms apps in WPF but I'm having an issue already with the menu.

I can't figure out why the menus in the WPF application open right to left, when the menus on the WinForms app open left to right (which is what I want):

enter image description here

It's a practically empty project with just the default settings. Here's the XAML:

<Window x:Class="WpfTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Test" Height="541.771" Width="828.947" WindowStartupLocation="CenterScreen">
    <DockPanel LastChildFill="False" Margin="0" Background="#FFFDFD9C">
        <Menu x:Name="menu" Height="22" VerticalAlignment="Top" RenderTransformOrigin="1.79,0.076" DockPanel.Dock="Top" Margin="0">
            <MenuItem Header="File">
                <MenuItem Header="New Document"/>
                <MenuItem Header="Open Template"/>
                <MenuItem Header="Open Collateral"/>
                <Separator/>
                <MenuItem Header="Synchronise"/>
                <Separator/>
                <MenuItem Header="Build Document"/>
                <MenuItem Header="Document History">
                    <MenuItem Header="Load Last Document"/>
                </MenuItem>
                <Separator/>
                <MenuItem Header="Settings"/>
                <Separator/>
                <MenuItem Header="Exit"/>
            </MenuItem>
            <MenuItem Header="Help">
                <MenuItem Header="User Guide"/>
                <MenuItem Header="About"/>
            </MenuItem>
        </Menu>
        <Rectangle Fill="#FFB8B8FF" Height="80" VerticalAlignment="Top" DockPanel.Dock="Top" Margin="0"/>
    </DockPanel>
</Window>

What am I missing?

19
  • 1
    All I can think of is a culture on your PC might be messing things up, try setting FlowDirection="LeftToRight" in your Window? Also if the Window is to the very right edge of the screen it will pop up on the other side.
    – XAMlMAX
    May 19, 2016 at 14:47
  • My culture is en-GB. FlowDirection didn't seem to make a difference. I added it within the <Window> tag, I assume this was correct? It's not at the very right of the screen, there's plenty of room for the menu to open.
    – Equalsk
    May 19, 2016 at 14:49
  • Do you need that RenderTransform in your Menu ? Or are you using designer to drag and drop things on screen?
    – XAMlMAX
    May 19, 2016 at 14:54
  • I've used the designer to drag items onto the form. The property was added automatically and I don't understand what it does so I left it. Removing it doesn't change anything.
    – Equalsk
    May 19, 2016 at 14:55
  • 2
    To whomever is downvoting, can you please leave a comment suggesting how this post can be improved. Stealth downvoting on what I believe is a clear question is not helpful.
    – Equalsk
    May 19, 2016 at 15:04

3 Answers 3

11

The way menus are opened depends on the SystemParameters.MenuDropAlignment property. This is usually set to align to the right if you have a tablet computer configured for right-handed people.

The setting can be changed for the computer in Control Panel -> Tablet PC Settings -> Specify which hand you write with. If you don't have Tablet PC Settings in your control panel you can try running shell:::{80F3F1D5-FECA-45F3-BC32-752C152E456E}. That should open Tablet PC Settings configuration dialog (source: https://stackoverflow.com/a/25109673/3506292). There is also a registry key you can create to change this setting.

You can use answers from this or that questions to override this behavior if it is absolutely required.

The easiest change (from https://stackoverflow.com/a/22065976/3506292)

private static readonly FieldInfo _menuDropAlignmentField;
static MainWindow()
{
    _menuDropAlignmentField = typeof(SystemParameters).GetField("_menuDropAlignment", BindingFlags.NonPublic | BindingFlags.Static);
    System.Diagnostics.Debug.Assert(_menuDropAlignmentField != null);

    EnsureStandardPopupAlignment();
    SystemParameters.StaticPropertyChanged += SystemParameters_StaticPropertyChanged;
}

private static void SystemParameters_StaticPropertyChanged(object sender, PropertyChangedEventArgs e)
{
    EnsureStandardPopupAlignment();
}

private static void EnsureStandardPopupAlignment()
{
    if (SystemParameters.MenuDropAlignment && _menuDropAlignmentField != null)
    {
        _menuDropAlignmentField.SetValue(null, false);
    }
}
4
  • 1
    I'm sorry but I don't really understand your answer. I'm not on a tablet, this is a standard desktop PC. On the same PC in a WinForms project the menus open correctly, it's only in WPF they're reversed. I'm sure I'm just missing a property somewhere, having to write a massive override method seems like overkill to me and isn't addressing the actual cause.
    – Equalsk
    May 19, 2016 at 15:06
  • @Equalsk It's very easy to identify. Just check the value of SystemParameters.MenuDropAlignment. If it is true, the popups are configured to appear right to left. On my machine the popups are opened right to left, and after I apply the solution from this answer they are appearing left to right. So it is the actual cause (at least for my machine).
    – filhit
    May 19, 2016 at 15:21
  • @Equalsk You can also check Control Panel -> Tablet PC Settings -> Specify which hand you write with (source answer). There are two options "Right-handed - Menus appear to the left of your hand." and "Left-handed - Menus appear to the right of your hand." Switch this radio button, press apply and see the changes in your app.
    – filhit
    May 19, 2016 at 15:32
  • 3
    I have the same issue on desktop PC (Windows 10) after connecting a second monitor with built-in touchscreen (Dell P2418HT). And I found a shorter version of your code: red-gate.com/simple-talk/blogs/…
    – SLA80
    Sep 21, 2020 at 10:33
2

I was able to implement the fix by using the code from @SLA80's link in their comment on the approved answer above.

Inside the App.xaml.cs, I added this code:

private static void SetDropDownMenuToBeRightAligned()
    {
        var menuDropAlignmentField = typeof(SystemParameters).GetField("_menuDropAlignment", BindingFlags.NonPublic | BindingFlags.Static);
        Action setAlignmentValue = () =>
        {
            if (SystemParameters.MenuDropAlignment && menuDropAlignmentField != null) menuDropAlignmentField.SetValue(null, false);
        };

        setAlignmentValue();

        SystemParameters.StaticPropertyChanged += (sender, e) =>
        {
            setAlignmentValue();
        };
    }

I call this function from the App constructor.

2

Very late to this show, but nonetheless here's my fix. Of course changing the registry entry solves the problem:

HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows

change

REG_SZ: MenuDropAlignment

according to your wishes. Value 0 means right, 1 is left.

But you can't suppose a user would look up and find this issue if your layout looks crappy. The fix is to apply a Datatrigger in the MenuItem Style. Look for the 'PART_Popup' in 'TopLevelHeaderTemplate' e.g.

 <Popup
    x:Name="PART_Popup"
    AllowsTransparency="true"
    Focusable="false"
    IsOpen="{Binding IsSubmenuOpen, RelativeSource={RelativeSource TemplatedParent}}"
    Placement="Bottom"
    PlacementTarget="{Binding ElementName=templateRoot}"
    PopupAnimation="{DynamicResource {x:Static SystemParameters.MenuPopupAnimationKey}}"
    >

And apply a DataTrigger:

 <DataTrigger Binding="{Binding Source={x:Static SystemParameters.MenuDropAlignment}}" Value="True">
     <Setter TargetName="PART_Popup" Property="Placement" Value="Left" />
     <Setter TargetName="PART_Popup" Property="HorizontalOffset" Value="{Binding ElementName=templateRoot, Path=ActualWidth, Converter={StaticResource NegativeConverter}}" />
     <Setter TargetName="PART_Popup" Property="VerticalOffset" Value="{Binding ElementName=templateRoot, Path=ActualHeight}" />
 </DataTrigger>

This worked for me, as changing the registry entry produces same result.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.