-2

I want to create a sort of a popup window like the Overloads window in Visual Studio. And just like the Overloads window, it can pop up pretty much anywhere on the screen.

I have a Window that has a child TextBlock control. The Window changes its size according to the TextBlock size by setting its SizeToContent property to WidthAndHeight. If the TextBlock Text is too long, the Window resizes its width beyond the screen's width resulting in a partially visible Window. I would like the Text to wrap when it reaches the end of the screen, so to say.

I tried setting the TextBlock TextWrapping property to Wrap to hopefully stop this behavior, but it doesn't help because of the SizeToContent property being set. Any suggestions/workarounds?

Thanks for your help!

Dinac23
  • 214
  • 2
  • 12

1 Answers1

2

You could set the MaxWidth property of the window to the width of the screen, e.g.:

TextBlock text = new TextBlock()
{
    Text = "some very long string...",
    TextWrapping = TextWrapping.Wrap
};
Window window = new Window()
{
    SizeToContent = SizeToContent.WidthAndHeight,
    Content = text,
    MaxWidth = System.Windows.SystemParameters.PrimaryScreenWidth
};
window.Show();

How to get the size of the current screen in WPF?

mm8
  • 163,881
  • 10
  • 57
  • 88
  • That would be the solution if the position of the `Window` were fixed, but the `Window` can appear pretty much anywhere on the screen (if the `Window` appears on the right side of the screen, setting the `MaxWidth` prop to `PrimaryScreenWidth` doesn't restrict it from overflowing). I'll add this explanation to the question to avoid further misconceptions. – Dinac23 Jun 08 '20 at 15:33
  • 2
    The solution is still to set the `MaxWidth`. You just need to calculate what to set it to. A hint: Look at the `Left` property of the window. – mm8 Jun 08 '20 at 15:35
  • I just would use mm8's answer, (MaxWidth=PrimaryScreenWidth-MyWindow.Left), Then I would just add the calculous to `LocationChanged` event. – Siegfried.V Jun 08 '20 at 16:36