[英]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 只会扩展到它的内容的大小。 如果滚动条可见性为“自动”,则不会显示滚动条,如果为“可见”,则会显示禁用的滚动条。 (注意,HorizontalScrollbarVisibility是默认的“已禁用”。)为了得到一个有用的水平滚动条,限制了ScrollViewer中的宽度和HorizontalScrollbarVisibility至少设置为“自动”。
我想补充。 如果您仍然没有看到滚动条,请将 PADDING 属性添加到 ScrollViewer。 这解决了我在应用程序中的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.