Miguel
Miguel

Reputation: 947

MessageBox resizing bahavior depending on the size of the Text string

Hi i am trying to implement the MessageBox resizing bahavior depending on the size of the Text (not caption) string in a custom DialogBox i am building in WPF. It's my custom MessageBox with the layout of my app.

But how MessageBox does to depending in the size of the string, the height of the MessageBox grows automatically? How to do that?

Thanks in advance!

Upvotes: 1

Views: 2657

Answers (2)

brunnerh
brunnerh

Reputation: 184516

This is how i normally do this:

<Window SizeToContent="WidthAndHeight" ResizeMode="NoResize" ...>

Additionally you could have a ScrollViewer as the child of the window and set the MaxHeight & MaxWidth properties on the window to restrict it further.

Edit: To give a discreet example of what the window might look like:

<Window
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  SizeToContent="WidthAndHeight"
  ResizeMode="NoResize" MaxWidth="400" MaxHeight="400">
  <Grid>
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <Grid.Children>
          <FlowDocumentScrollViewer ScrollViewer.VerticalScrollBarVisibility="Auto">
            <FlowDocument>
                <Paragraph>
                    <Run Text="{Binding DisplayText}"/>
                </Paragraph>
            </FlowDocument>
          </FlowDocumentScrollViewer>

          <StackPanel Grid.Row="1">
            <!-- Buttons -->
          </StackPanel>
    </Grid.Children>
  </Grid>
</Window>

Upvotes: 3

Marino Šimić
Marino Šimić

Reputation: 7342

In WPF you usually use the FormattedText class to finetune text.

What you need exactly if I got it right is the BuildGeometry method:

http://msdn.microsoft.com/en-us/library/system.windows.media.formattedtext.buildgeometry.aspx

So you need:

  • Create you formatted text
  • Build the geometry from it
  • Extract the bounding box
  • Resize you messagebox to this box size

Upvotes: 0

Related Questions