简体   繁体   English

WPF Grid 不显示滚动条

[英]WPF Grid not showing scroll bars

In .NET 3.5 I have a Grid in a Window.在 .NET 3.5 中,我在窗口中有一个网格。 I am populating this Grid with Buttons.我正在用按钮填充这个网格。 When the buttons fill the grid and go out of view the Grid does not show the scroll bar.当按钮填充网格并从视图中消失时,网格不会显示滚动条。 I have set the Grids vertical scroll to be visible but its still not showing.我已将 Grids 垂直滚动设置为可见,但仍未显示。

<Window x:Name="Window" x:Class="MergeToCheck.CheckList"
             xmlns:sys="clr-namespace:System;assembly=mscorlib"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" Loaded="Window_Loaded" ScrollViewer.VerticalScrollBarVisibility="Disabled"
                ResizeMode="NoResize" ShowInTaskbar="False" Topmost="True" WindowStyle="None" 
        Height="671" Width="846.299" BorderThickness="5">

    <Grid>
        <Grid x:Name="MyGrid" HorizontalAlignment="Left" Height="535" VerticalAlignment="Top" Width="736" Margin="10,63,0,0" ScrollViewer.CanContentScroll="True" ScrollViewer.HorizontalScrollBarVisibility="Visible">
            <Grid.Resources>
                <Style TargetType="{x:Type Panel}">
                    <Setter Property="Margin" Value="0,0,0,6" />
                </Style>
            </Grid.Resources>
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition/>
                <ColumnDefinition/>
                <ColumnDefinition/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
        </Grid>        
    </Grid>
</Window>

The code which adds the buttons:添加按钮的代码:

        CheckList CheckListCtrl = new CheckList();

        System.Windows.Controls.Button btn;
        int row = 0;
        int col = 0;

        CheckListCtrl.MyGrid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(100) });

        foreach(var c in list)
        {
            btn = new System.Windows.Controls.Button();
            btn.FontSize = 15;
            btn.FontWeight = FontWeights.UltraBold;
            btn.Content = c.Name;
            btn.Style = System.Windows.Application.Current.FindResource(System.Windows.Controls.ToolBar.ButtonStyleKey) as Style;
            btn.BorderBrush = new SolidColorBrush(Colors.Black);
            btn.BorderThickness = new Thickness(2);
            btn.MinWidth = 145;
            btn.MaxWidth = 145;
            btn.MinHeight = 95;
            btn.MaxHeight = 95;

            btn.SetValue(Grid.RowProperty, row);
            btn.SetValue(Grid.ColumnProperty, col);

            CheckListCtrl.MyGrid.Children.Add(btn);

            if ((col + 1) % CheckListCtrl.MyGrid.ColumnDefinitions.Count == 0)
            {                    
                col = 0;
                row++;
                CheckListCtrl.MyGrid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(100) });
            }
            else
                col++;
        }

Grid does not support scrolling functionality. Grid不支持滚动功能。 If you want to scroll something you need ScrollViewer control如果要滚动某些内容,则需要ScrollViewer控件

<ScrollViewer HorizontalScrollBarVisibility="Visible">
   <Grid x:Name="MyGrid" HorizontalAlignment="Left" Height="535" VerticalAlignment="Top" Width="736" Margin="10,63,0,0">
      <Grid.Resources>
         <Style TargetType="{x:Type Panel}">
            <Setter Property="Margin" Value="0,0,0,6" />
         </Style>
      </Grid.Resources>
      <Grid.ColumnDefinitions>
         <ColumnDefinition/>
         <ColumnDefinition/>
         <ColumnDefinition/>
         <ColumnDefinition/>
         <ColumnDefinition/>
      </Grid.ColumnDefinitions>
   </Grid>        
</ScrollViewer>

In general, a ScrollViewer needs to be told that it is smaller than its content.通常,需要告诉 ScrollViewer 它小于其内容。 So just adding a ScrollViewer to make a control scrollable is not always sufficient.因此,仅添加 ScrollViewer 使控件可滚动并不总是足够的。 The ScrollViewer knows that it is smaller if its enclosing control has a fixed or maximum size, or if itself has a fixed height or maximum height, as in ScrollViewer 知道如果其封闭控件具有固定或最大尺寸,或者自身具有固定高度或最大高度,则它更小,如

<ScrollViewer Height=500 HorizontalScrollBarVisibility="Visible">
...
</ScrollViewer>

, or if its Height (or MaxHeight) is bound to something appropriate. ,或者它的高度(或 MaxHeight)是否绑定到适当的值。

The same thing goes for the horizontal scrollbar, you can set it to visible all you like, if the width of the ScrollViewer is not constrained, the ScrollViewer will just expand to the size of its content.水平滚动条也是一样,你可以把它设置为你喜欢的可见,如果 ScrollViewer 的宽度没有限制,ScrollViewer 只会扩展到它的内容的大小。 If the scrollbar visibility then is "Auto", it will not show a scrollbar, and if it is "Visible", it will show a disabled one.如果滚动条可见性为“自动”,则不会显示滚动条,如果为“可见”,则会显示禁用的滚动条。 (Note that the HorizontalScrollbarVisibility is "Disabled" by default.) To get a useful horizontal scrollbar, constrain the width of the ScrollViewer and set its HorizontalScrollbarVisibility to at least "Auto". (注意,Horizo​​ntalScrollbarVisibility是默认的“已禁用”。)为了得到一个有用的水平滚动条,限制了ScrollViewer中的宽度Horizo​​ntalScrollbarVisibility至少设置为“自动”。

I would like to add.我想补充。 If you still do not see scrollbar add PADDING attribute to ScrollViewer.如果您仍然没有看到滚动条,请将 PADDING 属性添加到 ScrollViewer。 This fixed my problem in an app.这解决了我在应用程序中的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM