Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are my menus opening right to left instead of left to right?

Tags:

wpf

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?

like image 780
Equalsk Avatar asked May 19 '16 14:05

Equalsk


People also ask

How do I change Windows from right to left?

Select the window you want to snap and press the Windows Logo Key + Left Arrow or the Windows Logo Key + Right Arrow to snap the window to the side of the screen where you want it to be. You can also move it to a corner after snapping it.

How do I change my right click to the right?

Windows 10Right-click the Windows icon and select Search. Type mouse. Select Mouse Settings. Under the Select your primary button drop-down, choose Left or Right.


2 Answers

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);
    }
}
like image 148
filhit Avatar answered Oct 02 '22 19:10

filhit


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.

like image 35
user947737 Avatar answered Oct 02 '22 17:10

user947737