WPF网格未显示滚动条

本文关键字:滚动条 显示 网格 WPF | 更新日期: 2023-09-27 18:29:08

在.NET 3.5中,我在一个窗口中有一个网格。我正在用按钮填充此网格。当按钮填充网格并离开视图时,网格不会显示滚动条。我已经将网格垂直滚动设置为可见,但它仍然不显示。

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

添加按钮的代码:

        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++;
        }

WPF网格未显示滚动条

Grid不支持滚动功能。如果你想滚动一些东西,你需要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>

通常,需要告诉ScrollViewer它比它的内容小。因此,仅仅添加一个ScrollViewer使控件可滚动并不总是足够的。ScrollViewer知道,如果它的封闭控件具有固定或最大大小,或者如果它本身具有固定高度或最大高度,则它会更小,如

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

,或者其Height(或MaxHeight)是否绑定到适当的值。

水平滚动条也是如此,你可以随心所欲地将其设置为可见,如果ScrollViewer的宽度不受限制,ScrollViewer只会扩展到其内容的大小。如果滚动条可见性为"自动",则不会显示滚动条,如果为"可见",则会显示禁用的滚动条。(请注意,默认情况下,HorizontalScrollbarVisibility为"Disabled"(禁用)。)要获得有用的水平滚动条,请限制ScrollViewer的宽度将其HorizontalRollbarVisibility设置为至少"Auto"(自动)。

我想补充一下。如果仍然看不到滚动条,请将PADDING属性添加到ScrollViewer。这解决了我在应用程序中的问题。