1

我正在编写一个控件来显示和编辑表单中的对象。控件 ( FormDataView) 是一个ItemsControl,其中每个项目都是一个FormField由 组成的控件Grid,字段名称在左列,编辑器(例如 TextBox)在右列。为了对齐编辑器,我希望每个Grid中的第一列共享相同的宽度。

所以我尝试使用IsSharedSizeScopeand SharedSizeGroup,但它不起作用,第一列在每个FormField.

以下是这些控件的样式:

<Style TargetType="{x:Type ctl:FormDataView}" BasedOn="{StaticResource ResourceKey={x:Type ItemsControl}}">
    <Setter Property="ItemsPanel">
        <Setter.Value>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Vertical"
                            Grid.IsSharedSizeScope="True"
                            IsItemsHost="True" />
            </ItemsPanelTemplate>
        </Setter.Value>
    </Setter>
</Style>

<Style TargetType="{x:Type ctl:FormField}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ctl:FormField}">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" SharedSizeGroup="headerColumn" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>
                    <ContentPresenter Grid.Column="0"
                                      Content="{TemplateBinding Header}"
                                      Margin="3"
                                      TextElement.FontWeight="Bold" />
                    <ContentPresenter Grid.Column="1"
                                      Name="PART_Display"
                                      ContentTemplate="{TemplateBinding DisplayTemplate}"
                                      Margin="2"/>
                    <ContentPresenter Grid.Column="1"
                                      Name="PART_Editor"
                                      ContentTemplate="{TemplateBinding EditorTemplate}"
                                      Margin="2"
                                      Visibility="Collapsed" />
                </Grid>
                <ControlTemplate.Triggers>
                    <DataTrigger Binding="{Binding IsInEditMode, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ctl:FormDataView}}}"
                                 Value="True">
                        <Setter TargetName="PART_Display" Property="Visibility" Value="Collapsed" />
                        <Setter TargetName="PART_Editor" Property="Visibility" Value="Visible" />
                    </DataTrigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Notice how Grid.IsSharedSizeScope is set in the ItemsPanel of FormDataView, while SharedSizeGroup is set in the template of FormField. This correctly expresses what I want to do: each FormField should use the same width for the first column. However, according to the documentation for the SharedSizeGroup property, this scenario is not supported:

Grid size-sharing does not work if you set IsSharedSizeScope to true within a resource template and you define SharedSizeGroup as outside that template.

OK, so I can understand why it doesn't work... but I don't know how to work around this limitation.

Any idea?

N.B.: I don't want to assign a fixed width to the first column of course...

4

1 回答 1

3

Sadly I have no access to my Visual Studio Enviroment so I couldnt check the following tips...

  1. Assign Grid.IsSharedSizeScope="True" to FormDataView itself and not to the ItemsPanel. Do you really need StackPanel as the items panel? cant you live without that?

See if the above change works first...

  1. if not then revamp your item level code and assign SharedSizeGroup="headerColumn" in your item data template of your FormDataView and not in the ControlTemplate of individual FormField.

Let me know if this helps....

于 2011-07-30T08:58:14.010 回答