VariableSizedWrapGrid中的水平中心项

时间:2017-01-09 10:18:14

标签: c# xaml uwp win-universal-app uwp-xaml

我有一个VariableSizedWrapGrid,按钮作为项目:

<Page.Resources>
        <DataTemplate x:Key="LinkTemplate" x:DataType="local:LinkWeb">
            <Button Width="100" Height="100" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Margin="15,15,15,15" Click="Button_Click" >
                <Button.Content>                
                    <StackPanel>
                        <Image Source="{Binding ImageLink}" Margin="8,8,8,8" HorizontalAlignment="Center" Height="{Binding HeightImage}" Width="{Binding WidthImage}"/>                        
                        <TextBlock Text="{Binding Text}" TextWrapping="WrapWholeWords" />
                    </StackPanel>
                </Button.Content>            
            </Button>
        </DataTemplate>
    </Page.Resources>

...

<ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled" Grid.Row="1">
            <ItemsControl x:Name="imageContent" ItemsSource="{Binding Enlaces}" ItemTemplate="{StaticResource LinkTemplate}" >
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <VariableSizedWrapGrid Orientation="Horizontal" ScrollViewer.HorizontalScrollBarVisibility="Disabled" HorizontalChildrenAlignment="Stretch" MaximumRowsOrColumns="2"/>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
            </ItemsControl>
        </ScrollViewer>

VariableSizedWrapGrid每行显示两个项目,但项目显示为左侧对齐,如您所见:

enter image description here

我需要VariableSizedWrapGrid显示以其内容为中心的项目,如下所示:

enter image description here

关于如何实现这一目标的任何想法?

修改

我已尝试将ScrollViewer放在中心位置,是的,项目是#34;稍微集中一点&#34;,因为ScrollViewer中的VariableSizedWrapGrid居中,但是,在VariableSizedWrapGrid项目中浮动到左边,所以项目没有居中,你可以在这张照片中看到:

enter image description here

1 个答案:

答案 0 :(得分:2)

您可以将VariableSizedWrapGrid的Horizo​​ntalChildrenAlignment属性值设置为Center。它应该工作。

<ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <VariableSizedWrapGrid Orientation="Horizontal" ScrollViewer.HorizontalScrollBarVisibility="Disabled" HorizontalChildrenAlignment="Center" MaximumRowsOrColumns="2" />
        </ItemsPanelTemplate>
</ItemsControl.ItemsPanel>

enter image description here