views:

409

answers:

2

Is it possible to automatically resize a grid column in XAML when its contents are scaled?

Below are two screenshots to better explain what I mean. When the UserControl is first displayed it looks like:

before scaling

The intention is that the white rounded rectangle (with the textblock, combobox and slider) should always be positioned off to the right of the grey rectangle. However, when the grey rectangle is scaled up from the code behind, the grid column width does not increase to accomodate this and creates the overlap as seen below.

after scaling

Is there some way to make this column change width automatically to fit the controls inside from XAML?

My XAML currently looks like:

<UserControl
x:Class="Project.Items.Bubble"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Project.Items">
<UserControl.Resources>
    <ResourceDictionary
        Source="./Assets/BubbleResourceDictionary.xaml" />
</UserControl.Resources>
<Grid
    ShowGridLines="True">
    <Grid.ColumnDefinitions>
        <ColumnDefinition
            Width="Auto" />
        <ColumnDefinition
            Width="Auto" />
    </Grid.ColumnDefinitions>
    <Grid
        x:Name="ObjectRoot"
        Style="{StaticResource ObjectRootStyle}">
        <Rectangle
            Style="{StaticResource RectangleStyle}" />
        <Rectangle
            Style="{StaticResource HighlightStyle}" />
        <TextBlock
            Style="{StaticResource TextStyle}"
            Text="&lt;Text&gt;" />
    </Grid>
    <local:Editor
        x:Name="Editor"
        VerticalAlignment="Top"
        HorizontalAlignment="Right"
        Grid.Column="1" />
</Grid>

Note: This is in Silverlight.

+1  A: 

In Silverlight the approach would be to give the row and column definitions which define the cell a specific Height and Width respectively. Set the gray rectangle to Stretch to fill the cell. Now you can modify the Width and Height properties of the definitions and the other cells (and their contents) will move accordingly.

AnthonyWJones
Sorry for being slow at getting back to this. Resizing the grid row and column definitions makes sense. However, a different problem then shows up where the text doesn't get scaled. Is there a property to autosize text based on the size of its parent container? If not I may need to create a custom panel that resizes to the scaled control.
Jason
Take a look at the Toolkit ViewBox control, I believe scales content in the manner you are after. So wrap your content in this control and you should see the desired results.
AnthonyWJones
A: 

I fixed this by using the LayoutTransformer provided in the Silverlight Toolkit. I upated placed the XAML to be scaled inside the layout transformer tags as can be seen here:

<UserControl
x:Class="Project.Items.Bubble"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Project.Items"
xmlns:toolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Layout.Toolkit">
<UserControl.Resources>
    <ResourceDictionary
        Source="./Assets/BubbleResourceDictionary.xaml" />
</UserControl.Resources>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition
            Height="Auto" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition
            Width="Auto" />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <toolkit:LayoutTransformer
        x:Name="LayoutTransformer">
        <toolkit:LayoutTransformer.LayoutTransform>
            <ScaleTransform
                x:Name="ScaleTransform" />
        </toolkit:LayoutTransformer.LayoutTransform>
        <Grid
            x:Name="ObjectRoot"
            Grid.Row="1"
            Grid.Column="1"
            Style="{StaticResource ObjectRootStyle}">
            <Rectangle
                Style="{StaticResource RectangleStyle}" />
            <Rectangle
                Style="{StaticResource HighlightStyle}" />
            <TextBlock
                Style="{StaticResource BubbleTextStyle}"
                Text="&lt;Text&gt;" />
        </Grid>
    </toolkit:LayoutTransformer>
    <local:Editor
        x:Name="Editor"
        VerticalAlignment="Top"
        HorizontalAlignment="Right"
        Grid.Column="1" />
</Grid>

In the code behind I got rid of my RenderTransform and added an event that would fire when the scale should change. The handler looks like:

    private void MindBubbleScaleChanged(object sender, ScaleChangedEventArgs e)
    {
        ScaleTransform.ScaleX += e.Delta;
        ScaleTransform.ScaleY += e.Delta;
        LayoutTransformer.ApplyLayoutTransform();
    }
Jason