0

我在网格的第三列中有 Itemscontrol,它显示了一组动态加载的按钮。我希望这些内容(即按钮)占据网格的最大宽度。当内容超过网格大小时,将显示垂直滚动条。

我将滚动条样式应用于 ItemsControl,如下所示:

<Style x:Key="ItemControlStyle" TargetType="{x:Type ItemsControl}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ItemsControl}">
                        <Border>
                            <ScrollViewer HorizontalContentAlignment="Stretch"
                                          CanContentScroll="True"
                                          HorizontalScrollBarVisibility="Disabled"
                                          Uid="ScrollViewer_9"
                                          VerticalScrollBarVisibility="Auto">
                                <ItemsPresenter Margin="{TemplateBinding Padding}"
                                                KeyboardNavigation.DirectionalNavigation="Cycle"
                                                SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                                Uid="ItemsPresenter_5" />
                            </ScrollViewer>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

我还为 ItemsControl 及其父级(即 Grid)应用了 Horizo​​ntalAlignment 和 VerticalAllignMent 作为“拉伸

我想要的输出视图是(网格的第三列) 在此处输入图像描述

我得到的输出是: 在此处输入图像描述

尺寸超过后应该出现滚动条如何将这些内容水平调整到网格的最大宽度?

4

1 回答 1

1

这只是添加HorizontalAlignment=Stretch到的情况ScrollViewer吗?

IE:

<Style x:Key="ItemControlStyle" TargetType="{x:Type ItemsControl}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ItemsControl}">
                <Border>
                    <ScrollViewer  HorizontalAlignment="Stretch"
                                   HorizontalContentAlignment="Stretch"
                                   CanContentScroll="True"
                                   HorizontalScrollBarVisibility="Disabled"
                                   Uid="ScrollViewer_9"
                                   VerticalScrollBarVisibility="Auto">
                         <ItemsPresenter Margin="{TemplateBinding Padding}"
                                         KeyboardNavigation.DirectionalNavigation="Cycle"
                                         SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                         Uid="ItemsPresenter_5" />
                    </ScrollViewer>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
于 2012-10-22T08:37:08.367 回答