滚动条在GridCotrol网格上显示错误

时间:2012-10-22 07:27:37

标签: wpf xaml scrollbar itemscontrol

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

我将Scrollbar样式应用于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 作为“Stretch”。

我想要的输出视图是(网格的第3列) enter image description here

我得到的输出是: enter image description here

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

1 个答案:

答案 0 :(得分:1)

是否只是将 HorizontalAlignment=Stretch 添加到ScrollViewer

即:

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