Search code examples
wpfsizetocontent

WPF Window Size to... most of content?


I know WPF windows have the 'SizeToContent' property and it works great and is almost what I want in this case. However what I have is slightly different. I have a window that has some general content on it and I want it to automatically size to the height of that content. Set SizeToContet="Height"... so far so good.

However, I also have a graphic element that is behind the main content and designed to go in the lower left corner. This window can be short so in some cases the graphic is taller than the main content; in this case I would like it to clip the graphic to the height of the main content. However with SizeToContent it sees the graphic as part of the content (understandably) and won't let the window go shorter than that.

Is there a way to tell a specific item to be ignored when calculating SizeToContent height?


Solution

  • Ok I think I figured out something that will work. The Background property of UI elements don't contribute to this calculation it seems, so what I ended up doing is putting an ImageBrush background on the main grid like this:

        <Grid.Background>
            <ImageBrush ImageSource="Images/Gfx_UICorner_NoLogo.png" AlignmentX="Left" AlignmentY="Bottom" Stretch="None" />
        </Grid.Background>
    

    Doing this will properly align it to bottom left and it will not contribute to calculating the desired size of the window so short windows will then cut off the top like I was looking for.

    Thanks to this thread for pointing me in the right direction...