dotnet / wpf

WPF is a .NET Core UI framework for building Windows desktop applications.
MIT License
7.08k stars 1.17k forks source link

Inconsistent visual for Wpf grid showing black line on right depending on size #9816

Open ejohnson-dotnet opened 1 month ago

ejohnson-dotnet commented 1 month ago

Description

I have a simple WPF app with a grid showing textblocks and textboxes.

If the textblock is a certain length, the window shows a black vertical bar on the right. Adding one character to the textblock eliminates the black bar.

Here is the Xaml code for the window. If you add one character or remove one character from the textblock, the line on the right goes away.

I have screen shots of the window below.

<Window x:Class="MyApp.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:MyApp"
        mc:Ignorable="d"
        Title="Title" SizeToContent="WidthAndHeight" WindowStartupLocation="CenterScreen" ResizeMode="CanMinimize" >
    <Grid  >
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>

        <TextBlock Grid.Row="0" Margin="2" Text="123456789 123456789 1234567" />
        <TextBlock Grid.Row="1" Margin="2" Text="123456789 123456789" />
        <TextBlock Grid.Row="2" Margin="2" Text="123456789 123456789" />
        <TextBlock Grid.Row="3" Margin="2" Text="123456789 123456789" />
        <TextBlock Grid.Row="4" Margin="2" Text="123456789 123456789" />

        <TextBox Grid.Row="0" Grid.Column="1" Margin="2" MaxLength="10" Width="90" />
    </Grid>
</Window>

This window shows a black bar on the right Screenshot 2024-09-19 161056

This window does not show the bar. The only difference is the text length. Screenshot 2024-09-19 161138

Reproduction Steps

Use this code in a WPF Xaml window:

<Window x:Class="MyApp.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:MyApp"
        mc:Ignorable="d"
        Title="Title" SizeToContent="WidthAndHeight" WindowStartupLocation="CenterScreen" ResizeMode="CanMinimize" >
    <Grid  >
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>

        <TextBlock Grid.Row="0" Margin="2" Text="123456789 123456789 1234567" />
        <TextBlock Grid.Row="1" Margin="2" Text="123456789 123456789" />
        <TextBlock Grid.Row="2" Margin="2" Text="123456789 123456789" />
        <TextBlock Grid.Row="3" Margin="2" Text="123456789 123456789" />
        <TextBlock Grid.Row="4" Margin="2" Text="123456789 123456789" />

        <TextBox Grid.Row="0" Grid.Column="1" Margin="2" MaxLength="10" Width="90" />
    </Grid>
</Window>

Expected behavior

It should not show the black vertical bar on the right side of the window.

Actual behavior

It is showing a vertical black bar on the right side of the window.

Regression?

I have tried .NET 4.8, .NET 7 and .NET 8. All produce the same problem.

Known Workarounds

No response

Impact

No response

Configuration

.NET 8, C#, Windows 11 Pro x64

Other information

No response

hongruiyu commented 1 month ago

You could try setting SnapsToDevicePixels to True

<Window  ... SnapsToDevicePixels="True">
harshit7962 commented 1 month ago

@ejohnson-dotnet does the above settings helps solve your problem?

miloush commented 1 month ago

Please attach a repro project. If it is a rounding issue, you can also set UseLayoutRounding=True. Note though that there isn't any element with borders, it feels like it's the window surface that is not aligned with the native window and if that's the case I think should be fixed even if workarounds were available.

ejohnson-dotnet commented 1 month ago

Here is a project to reproduce the issue.

Yes, setting 'SnapsToDevicePixels' or 'UseLayoutRounding' to 'True' does fix the issue.

MyWpfApp.zip