簡體   English   中英

WPF Grid 不顯示滾動條

[英]WPF Grid not showing scroll bars

在 .NET 3.5 中,我在窗口中有一個網格。 我正在用按鈕填充這個網格。 當按鈕填充網格並從視圖中消失時,網格不會顯示滾動條。 我已將 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>

添加按鈕的代碼:

        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>

通常,需要告訴 ScrollViewer 它小於其內容。 因此,僅添加 ScrollViewer 使控件可滾動並不總是足夠的。 ScrollViewer 知道如果其封閉控件具有固定或最大尺寸,或者自身具有固定高度或最大高度,則它更小,如

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

,或者它的高度(或 MaxHeight)是否綁定到適當的值。

水平滾動條也是一樣,你可以把它設置為你喜歡的可見,如果 ScrollViewer 的寬度沒有限制,ScrollViewer 只會擴展到它的內容的大小。 如果滾動條可見性為“自動”,則不會顯示滾動條,如果為“可見”,則會顯示禁用的滾動條。 (注意,Horizo​​ntalScrollbarVisibility是默認的“已禁用”。)為了得到一個有用的水平滾動條,限制了ScrollViewer中的寬度Horizo​​ntalScrollbarVisibility至少設置為“自動”。

我想補充。 如果您仍然沒有看到滾動條,請將 PADDING 屬性添加到 ScrollViewer。 這解決了我在應用程序中的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM