Odd behavior with WPF window position and scale greater than 100%

299 views Asked by At

I have a three-monitor setup: the left one is at 1920x1080 at 100%, the center and right ones are at 3840x2160 at 150%.

When I set the Left property of the window to 2487, it appears halfway between the middle and right monitors. When I set the Left property to 2488, the Window jumps near to the middle of the center monitor. This makes no sense to me, even if I take into account DPI.

If I set the monitor scale to 100%, it behaves as expected. I'm just trying to figure out how to tell when a window is close to the right edge of a screen, and this is making it impossible.

XAML:

<Window x:Class="WpfApp2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp2"
        mc:Ignorable="d"
        Title="MainWindow" Height="100" Width="300">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <TextBox x:Name="XLocationBox" Grid.Column="0" HorizontalAlignment="Stretch" VerticalAlignment="Center"></TextBox>
        <Button Click="Button_Click" Grid.Column="1" Width="40" Height="30">Set</Button>
    </Grid>
</Window>

Code:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        Left = Convert.ToDouble(XLocationBox.Text);
    }
}
0

There are 0 answers