tags:

views:

58

answers:

1

If you run this code and click on tab 2 and then back on tab 1 the application goes insane and starts bouncing the column widths back and forth. Any suggestions on how to fix this?

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow"
    Height="350"
    Width="525">
<Grid IsSharedSizeScope="True">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="Auto"
                          SharedSizeGroup="Col3" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>

    <TextBlock Text="Label 1"
               Grid.Row="0"
               Grid.Column="0" />

    <TextBox Grid.Column="1"
             Grid.Row="0"
             Text="TextBox 1" />

    <TextBlock Text="Label 2"
               Grid.Row="0"
               Grid.Column="2" />

    <TextBox Grid.Column="3"
             Grid.Row="0"
             Text="TextBox 2" />

    <DockPanel Grid.Row="1"
               Grid.Column="2"
               Grid.ColumnSpan="2">

        <TabControl>
            <TabItem Header="Tab 1">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition SharedSizeGroup="Col3" />
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>
                    <TextBlock Text="Tab 1: Short Text.."
                               Grid.Row="0"
                               Grid.Column="0" />

                </Grid>
            </TabItem>
            <TabItem Header="Tab 2">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition SharedSizeGroup="Col3" />
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>
                    <TextBlock Text="Tab 2: Short Text.."
                               Grid.Row="0"
                               Grid.Column="0" />
                    <TextBlock Text="Tab 2: Long Text..................................... "
                               Grid.Row="1"
                               Grid.Column="0" />
                </Grid>
            </TabItem>
        </TabControl>
    </DockPanel>
    </Grid>
</Window>

This is stripped from a similar application and greatly simplified. The root of the problem is the SharedSizeGroup "Col3". In the actual application there are other items that share that column so I cannot remove SharedSizeGroup unless there is another way to accomplish the desired behavior.

+2  A: 

Setting attached property Grid.IsSharedSizeScope to True on parent element (TabControl) should stop size sharing propagation above the hierarchy. Like so:

<TabControl Grid.IsSharedSizeScope="True">

Within TabControl the sizes will be aligned.

repka
Thanks for your answer but it doesn't solve my problem. That would work fine but I need the shared size scope to be outside of the TabControl. In my example I need it to share with the label in the grid outside of the TabControl.
Shaun Bowe
Oh... from your description earlier I figured that you didn't control either the content of TabControl or the shell hosting it and wanted two things to not affect each other. Now that you're saying that you actually *do* want to share the size between column and a control inside of it, I'm confused. From your code I see that DockPanel is placed inside the parent grid’s 2nd column which has shared size with a child grid inside TabControl. If you’re using panels like Grid or DockPanel which take all available space inside your TabItem, how do you expect this size sharing to work?...
repka
...In other words you want the column to be the same size as column inside it and some more (i.e. other columns and borders around TabControl/TabItems). There’s no way to solve equation X = X + some positive value. Please elaborate on what you’re trying to achieve. It’s doable, if you place sub-grids of your TabItems into something like StackPanel with Orientation=Horizontal, but still doesn’t make sense to me.
repka
Changing the DockPanel to a StackPanel solved the problem. Now that I think about it that makes total sense. Thanks a lot.
Shaun Bowe