tags:

views:

63

answers:

1

I'm creating a modal dialog window which contains three essential parts: a TextBlock containing instructions, a ContentControl for the dialog panel, and a ContentControl for the dialog buttons. Each of these parts are contained in a separate Grid row.

I have some specific constraints when it comes to how the dialog should be sized. The issue I'm having is with the instructions TextBlock. I want the instructions to be as wide as the ContentControl for the dialog panel. The instructions should then wrap and grow vertically as needed. Should the instructions not be able to grow vertically, then it should begin to grow horizontally.

Getting the instructions to be the width of the ContentControl and grow vertically was simple. The part I can't seem to figure out is how to get it to grow horizontally when out of vertical space. My initial thought was to create a class that extends TextBlock and override MeasureOverride. However, that method is sealed. Currently, I'm playing with the idea of have the dialog Window override MeasureOverride to calculate the available size for the instructions block.

Am I missing a much simpler way of accomplishing this? Does anyone have any better ideas than this? Messing with MeasureOverride seems like it will be a lot of work.

Here is some sample code to give you a general idea of how the dialog is laid out:

<Window
    x:Class="Dialogs.DialogWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Name="dialogWindow"
    ShowInTaskbar="False"
    WindowStyle="None"
    AllowsTransparency="True"
    Background="Transparent"
    ResizeMode="NoResize"
    SizeToContent="WidthAndHeight"
    WindowStartupLocation="CenterScreen">

    <Border Style="{StaticResource WindowBorderStyle}" Margin="15">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>

            <TextBlock
                Margin="25,5"
                VerticalAlignment="Top"
                HorizontalAlignment="Left"
                Text="{Binding Instructions}"
                TextWrapping="Wrap"
                Width="{Binding ElementName=panelContentControl, Path=ActualWidth, Mode=OneWay}"/>

            <ContentControl
                x:Name="panelContentControl"
                Grid.Row="1"
                Margin="25,5"
                Content="{Binding PanelContent}"/>

            <ContentControl
                x:Name="buttonsContentControl"
                Grid.Row="2"
                HorizontalAlignment="Right"
                VerticalAlignment="Center"
                Margin="25,5"
                Content="{Binding ButtonsContent}"/>
        </Grid>
    </Border>

</Window>
+1  A: 

It appears as if what you really want to create a new Panel or derive from an existing one which would take place of what is currently your Grid. Panel is responsible for laying out your content so you should go that way instead of messing with Window.MeasureOverride.

How exactly do you want your TextBlock to grow horizontally and why do you want this? By growing it horizontally do you want it to grow the Window too?

wpfwannabe
Yes, the window would need to grow horizontally as well. The reason it should grow vertically first is so that the instructions stay as wide as the dialog's panel content and so that the panel content does not need to grow unless necessary. The TextBlock would grow horizontally if the dialog window could no longer grow vertically.
RFBoilers