WPF: Order of stretch sizing

Posted by RFBoilers on Stack Overflow See other posts from Stack Overflow or by RFBoilers
Published on 2010-04-27T20:36:46Z Indexed on 2010/04/27 21:33 UTC
Read the original article Hit count: 310

Filed under:
|
|

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>

© Stack Overflow or respective owner

Related posts about wpf

Related posts about c#