在.NET 3.5中,我在窗口中有一个网格。我用Buttons填充这个Grid。当按钮填满网格并离开视图时,网格不显示滚动条。我已将网格垂直滚动设置为可见但仍未显示。
<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++;
}
答案 0 :(得分:65)
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>
答案 1 :(得分:1)
通常,需要告知ScrollViewer小于其内容。因此,仅添加ScrollViewer来使控件可滚动并不总是足够的。 ScrollViewer知道,如果其封闭控件具有固定或最大尺寸,或者其自身具有固定高度或最大高度,则它较小,如
<ScrollViewer Height=500 HorizontalScrollBarVisibility="Visible">
...
</ScrollViewer>
,或者它的高度(或MaxHeight)是否绑定到适当的值。
水平滚动条也是如此,您可以将其设置为可见,如果ScrollViewer的宽度不受限制,则ScrollViewer只会扩展到其内容的大小。如果滚动条的可见性为“自动”,则不会显示滚动条,如果滚动条的可见性为“可见”,则将显示禁用的滚动条。 (请注意,默认情况下,HorizontalScrollbarVisibility处于“禁用”状态。)要获得有用的水平滚动条,请限制ScrollViewer 的宽度,并将的HorizontalScrollbarVisibility至少设置为“自动”。
答案 2 :(得分:0)
我想补充。如果仍然看不到滚动条,则将PADDING属性添加到ScrollViewer。 这解决了我在应用程序中的问题。