WPFDarkTheme icon indicating copy to clipboard operation
WPFDarkTheme copied to clipboard

SizeToContent "WidthAndHeight" breaks untill redraw

Open Hi-ImKyle opened this issue 1 year ago • 6 comments

Using a Window with SizeToContent="WidthAndHeight" causes some unwanted black space around the window on the bottom and right.

image

With SizeToContent="Width" it's fine. image

With SizeToContent="Height" it's also fine. image

Is this a Theme problem or a WPF problem? Without the theme it works fine, the issue doesn't occur. The second I put Style="{StaticResource CustomWindowStyle}" on the window, it breaks with WidthAndHeight

Hi-ImKyle avatar May 11 '24 21:05 Hi-ImKyle

This issue is with the WindowChrome that the style uses, in order to use a custom titlebar. A few workarounds I can think of is maybe calling Dispatcher.InvokeAsync(() => InvalidateVisual(), DispatcherPriority.Background) in the window's loaded event or inside a task (with say a half second delay beforehand)

AngryCarrot789 avatar May 11 '24 21:05 AngryCarrot789

While that does get rid of the black bits, it forces it to expand to fill the black parts. Which is not exactly what I wanted, but I can certainly live with it if you have no other ideas.

Hi-ImKyle avatar May 11 '24 21:05 Hi-ImKyle

Coming back to this, I don't think your solution actually fixed anything, just prevents the black parts from showing. The window doesn't actually size correctly according to WidthAndHeight, It's always slightly larger width and height wise :/

Hi-ImKyle avatar May 20 '24 13:05 Hi-ImKyle

Maybe if you set SizeToContent to Width, then Measure() the window and save the width, then set SizeToContent to Height and do the same measurement and save the height, then manually set the window's width and height to that?

Or I suppose just measure the window and set the width and height to the measured size, since Measure() is supposed to return the smallest amount of space a control will take up.

AngryCarrot789 avatar Jul 25 '24 16:07 AngryCarrot789

It's tacky and the underlying issue should just get fixed but this works. I can live with that.

Hi-ImKyle avatar Jul 31 '24 16:07 Hi-ImKyle

Apparently, setting a negative margin on the outmost border of affected window seems to help.

<Style.Resources>
    <Style TargetType="Border">
        <Setter Property="Margin" Value="-1"/>
    </Style>
</Style.Resources>

I am using this to override the window template set by Controls.xaml.

shanto avatar Sep 19 '24 13:09 shanto