Milen Grigorov
Milen Grigorov

Reputation: 352

How to share ColumnDefinition Width size using ControlTemplate?

I am using ControlTemplate as template for a few different UserControls and I want to share width of the columns from the template with the UserControl. I am trying with "SharedSizeGroup". I have the following ControlTemplate:

<ControlTemplate x:Key="baseTemplate" TargetType="UserControl">
        <ScrollViewer HorizontalContentAlignment="Stretch"
                HorizontalScrollBarVisibility="Disabled" VerticalAlignment="Stretch"
                  VerticalScrollBarVisibility="Disabled">
            <Grid Grid.Row="0">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <GroupBox x:Name="groupBoxAttributes" Header="Attributes"
                      HorizontalAlignment="Stretch" VerticalAlignment="Top" Grid.Row="0" Grid.Column="0">
                    <Grid  Grid.IsSharedSizeScope="True">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition SharedSizeGroup="attr"/>
                                <ColumnDefinition Width="Auto"/>
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                            </Grid.RowDefinitions>
                            <Label Content="NodeClass:" Grid.Row="0"/>
                            <TextBox x:Name="txtNodeClass" Margin="0" HorizontalAlignment="Stretch"
                             Height="25" TextWrapping="NoWrap" VerticalAlignment="Top" Grid.Row="0" Grid.Column="1"/>
                        </Grid>
                        <ContentPresenter  Grid.Row="1"
                                        HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                                        SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" 
                                        VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    </Grid>
                </GroupBox>
            </Grid>
        </ScrollViewer>
    </ControlTemplate>

And I am using it in UserControl as template:

<UserControl x:Class="myClass"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             Template="{DynamicResource baseTemplate}"
             Grid.IsSharedSizeScope="True"
             d:DesignHeight="447.533" d:DesignWidth="300">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" SharedSizeGroup="attr"/>
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Label Content="Type Definition:" Grid.Row="0"/>
        <ComboBox Height="25"  VerticalAlignment="Top" Grid.Row="0" Grid.Column="1"/>
    </Grid>
</UserControl>

I want the columns in the grids with SharedSizeGroup="attr" to be the same width, but I cannot get it to work.

Upvotes: 0

Views: 234

Answers (1)

mm8
mm8

Reputation: 169200

Remove the Grid.IsSharedSizeScope attached property from the template and set it on the UserControl element:

<UserControl Grid.IsSharedSizeScope="True" Template="{StaticResource baseTemplate}">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" SharedSizeGroup="attr"/>
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Label Content="Type Definition:" Grid.Row="0"/>
        <ComboBox Height="25"  VerticalAlignment="Top" Grid.Row="0" Grid.Column="1"/>
    </Grid>
</UserControl>

Upvotes: 2

Related Questions