c# - WPF 网格不显示滚动条

标签 c# wpf grid .net-3.5 scrollbar

在 .NET 3.5 中,我在窗口中有一个网格。我用按钮填充这个网格。当按钮填满网格并离开 View 时,网格不显示滚动条。我已将网格垂直滚动设置为可见,但仍未显示。

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

最佳答案

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>

关于c# - WPF 网格不显示滚动条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26737143/

相关文章:

c# - iTextSharp 文件操作和显示到 FileStreamResults

wpf - MVVM - 删除记录时关闭选项卡

.net - WPF - 如何制作一个画笔来绘制像方格纸一样的正方形?

php - 使用 php 在 SlickGrid 中保存更改

c# - 为什么我不能使用包含自 Action 为参数或返回类型的 DLL 中的函数?

c# - 在没有参数的另一个函数中使用一个函数的变量?

c# - 如果文件不存在,我的方法应该抛出自己的异常,还是让 .NET 抛出异常?

c# - 控件的事件到外部静态类方法

c# - System.Windows.Media.Media3D.Matrix3D 的工作原理

html - 网格内的 Bootstrap 3 内联导航栏