You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过ControlTemplate共享ColumnDefinition宽度?SharedSizeGroup未生效

解决ControlTemplate与UserControl之间SharedSizeGroup不生效的问题

我来帮你搞定这个SharedSizeGroup不生效的问题——核心原因是你不小心创建了两个独立的共享尺寸作用域,导致模板和UserControl内容里的列没法共享宽度。下面是具体的分析和解决方案:

问题根源

你在两处设置了Grid.IsSharedSizeScope="True":

  • ControlTemplate内部的Grid单独开启了共享作用域
  • UserControl本身也开启了共享作用域

这就导致模板里的attr列只在模板内部的Grid作用域生效,而UserControl自定义Content里的attr列属于UserControl的独立作用域,两者完全隔离,自然无法共享宽度。

解决方案:统一共享作用域

我们只需要在**最顶层(UserControl或其父容器)**设置一次Grid.IsSharedSizeScope="True",让模板和UserControl内容处于同一个作用域下即可。

修改后的ControlTemplate代码

移除模板内部Grid的Grid.IsSharedSizeScope="True",保留SharedSizeGroup标记:

<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.IsSharedSizeScope="True" -->
                <Grid>
                    <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层面的Grid.IsSharedSizeScope="True",确保整个视觉树处于同一个共享作用域:

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

拓展:多个UserControl实例共享宽度

如果你的需求是让多个UserControl实例之间的attr列也保持宽度一致,只需要把Grid.IsSharedSizeScope="True"移到它们的父容器上即可,比如:

<!-- 父容器开启共享作用域,所有子UserControl的attr列都会统一宽度 -->
<StackPanel Grid.IsSharedSizeScope="True">
    <local:myClass />
    <local:myClass />
    <local:myOtherUserControl />
</StackPanel>

内容的提问来源于stack exchange,提问作者Milen Grigorov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 03:53:37