2

ここで小さな WPF アプリを作成しています。これはすべて、ビュー モデル タイプにリンクされた DataTemplates のみを使用して、MVVM で厳密に構築されています。

ListBox項目がその親を満たすように es の内容を伸ばして切り取る方法について多くの質問を見てきました。多くの実験の後、私はそれを回避することができましたが、今では同じシナリオで自分自身を見つけましたItemsControlが、同じトリックはうまくいかないようです.

使用中の の 1 つDataTemplate(単純なTextBox) を次に示します。私がどのように設定しようとしたかに注意してくださいHorizontalAlignment...

<DataTemplate DataType="{x:Type vm:OneOfMyViewModelTypes}">
    <TextBox 
        Text="{Binding Path=Value}"
        HorizontalAlignment="Stretch"
        />
</DataTemplate>

これがItemsControl中身Gridです...

<Grid Background="Gray">
            <Grid.Margin>
                <Thickness 
                    Left="{StaticResource ConfigurationDefaultMargin}" 
                    Right="{StaticResource ConfigurationDefaultMargin}" 
                    Bottom="{StaticResource ConfigurationDefaultMargin}" 
                    Top="{StaticResource ConfigurationDefaultMargin}" 
                    />
            </Grid.Margin>
            <Grid.ColumnDefinitions>
                <ColumnDefinition SharedSizeGroup="_key" Width="Auto"/>
                <ColumnDefinition SharedSizeGroup="_value" Width="*"/>
            </Grid.ColumnDefinitions>

            <ItemsControl 
                Background="DimGray" 
                Grid.IsSharedSizeScope="True"
                ItemsSource="{Binding Path=Configuration, Mode=OneWay}" 
                HorizontalAlignment="Stretch" 
                HorizontalContentAlignment="Stretch"
                >
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition SharedSizeGroup="_key"/>
                                <ColumnDefinition SharedSizeGroup="_value"/>
                            </Grid.ColumnDefinitions>
                            <TextBlock
                                Style="{StaticResource ExtensionConfigurationLabel}"
                                Grid.Column="0"
                                Margin="5,5,5,0"
                                Text="{Binding Path=Caption}" 
                                />
                            <ContentPresenter 
                                Grid.Column="1"
                                HorizontalAlignment="Stretch"
                                Margin="5,5,5,0"
                                Content="{Binding}" 
                                />
                        </Grid>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </Grid>

コントロールのサイズを確認するために色を使用しました。はGridグレー、ItemsControlはダークグレーです。

これが結果です...

これが結果です

色からわかるように、含まれている部分は伸びますが、Grid伸びItemsControlません。HorizontalAlignmentプロパティをに設定しましStretchたが、効果がないようです。他に何かする必要がありますか?

ありがとう

4

1 に答える 1

2

You have two columns in your main (outer) grid, yet you use only the first column. The second column uses all the remaining space.

于 2012-04-30T09:09:04.550 に答える