question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SizeToContent not rendered correctly

See original GitHub issue

I’m new to this project, so my apologies in advance if this has been covered elsewhere.

It apears that MaterialWindow does not render the correct dimensions when using SizeToContent.

For example…

<mde:MaterialWindow x:Class="ClrVpin.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:clrVpin="clr-namespace:ClrVpin"
        xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
        xmlns:mde="https://spiegelp.github.io/MaterialDesignExtensions/winfx/xaml"
        mc:Ignorable="d"
        d:DataContext="{d:DesignData }"
        WindowStartupLocation="CenterScreen"
        ResizeMode="NoResize"
        SizeToContent="WidthAndHeight"
        Width="300" Height="250"
        Title="ClrVpin" WindowState="Normal">

Yields the MaterialWindow plus also the underlying Window… image

My workaround is to manually specify Width and Height, but this is a little painful for windows that I’d prefer dynamically size to the content.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:5

github_iconTop GitHub Comments

1reaction
stojycommented, Sep 18, 2021

Hi @markmov, my apologies for not replying sooner… but the email notification got lost in my spam folder 😦

Thanks for the tip. The Loaded handler works a treat…

Loaded += (_, _) => SizeToContent = SizeToContent.WidthAndHeight;

Regarding the extra pixel at the bottom, is this the sort of artifact you’re seeing without applying the ContentRendered hack? image

If so, try this…

  1. UseLayoutRounding="True" in xaml (or code behing)
  2. Remove ContentRendered handler. It should now be unnecessary (and expensive as I suspect it would cause a re-render)

And as a bonus, it should also remove the pixel artifacts in the nav bar too (only noticable if you’re using a dark theme heading style). image

Essentially I suspect the root issue here is the windows scaling that is being applied, especially for users on high resolution screens (eg. 4k).

0reactions
stojycommented, Sep 18, 2021

One thing I’ve just noticed… whilst explicitly assiging the SizeToContent within the Loaded event does assign the correct with/height, it unfortunately breaks WindowStartupLocation CentreScreen/Owner. It looks like the window isn’t repositioned to account for the (reduced) size.

Whilst it’s possible to offset this with a bit of math, it looks like making a similar change in ContentRendered is simpler as it doesn’t suffer the same problem.

public class MaterialWindowEx : MaterialWindow
    {
        // workarounds for MaterialDesignExtensions layout issue supporting SizeToContent.WidthAndHeight
        // - refer https://github.com/spiegelp/MaterialDesignExtensions/issues/144
        public MaterialWindowEx()
        {
            // removes unnecessary pixels in window: header, right, and bottom
            UseLayoutRounding = true;
            
            ContentRendered += (_, _) =>
            {
                if (SizeToContent != SizeToContent.Manual)
                {
                    // force a SizeToContent change so that WPF/MDE can correctly layout the window to fit the content
                    var sizeToContent = SizeToContent;
                    SizeToContent = SizeToContent.Manual;
                    SizeToContent = sizeToContent;
                }
            };
        }
    }

Plus the code above will only perform the content sizing if it’s needed, e.g. in xaml SizeToContent=SizeToContent.WidthAndHeight

Read more comments on GitHub >

github_iconTop Results From Across the Web

WPF SizeToContent not working when WindowStyle="None"
I am trying to remove all chrome (border and title). I can do this however the width does not size correctly. <Window x:Class="ActiveWords....
Read more >
SizeToContent not sizing correctly · Issue #2471
I have made a message box for one of my programs but it is not sizing as it should I have got SizeToContent="WidthAndHeight"...
Read more >
[BUG] Rendering bug when SizeToContent is set. #378
If I allow resize on the window, as soon as I drag the corner the layout updates and the problem goes away. Not...
Read more >
RadWindow with SizeToContent="True" not resized
I am trying to use RadWindow as a Dialog window with XAML similar to this: <Grid> <Grid.RowDefinitions> <RowDefinition/>
Read more >
SizeToContent concerns with RadWindow in UI for WPF
Hi Greg, Setting the SizeToContent property while having a specific Width and Height set is not possible in the current version of RadWindow ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found