如何使用ControlTemplate共享ColumnDefinition宽度大小?

时间:2019-02-19 12:16:18

标签: c# wpf

我正在将ControlTemplate用作几个不同的UserControls的模板,并且我想与UserControl共享模板中列的宽度。 我正在尝试使用“ SharedSizeGroup”。 我有以下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>

我正在UserControl中将其用作模板:

<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>

我希望具有SharedSizeGroup =“ attr”的网格中的列具有相同的宽度,但是我无法使其正常工作。

1 个答案:

答案 0 :(得分:2)

从模板中删除附加的Grid.IsSharedSizeScope属性,并将其设置在UserControl元素上:

<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>