WPF Grid not showing scroll bars(WPF Grid不显示滚动条)
问题描述
在 .NET 3.5 中,我在窗口中有一个网格.我正在用按钮填充这个网格.当按钮填满网格并消失时,网格不会显示滚动条.我已将网格垂直滚动设置为可见,但仍未显示.
In .NET 3.5 I have a Grid in a Window. I am populating this Grid with Buttons. When the buttons fill the grid and go out of view the Grid does not show the scroll bar. I have set the Grids vertical scroll to be visible but its still not showing.
<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
控件
Grid
does not support scrolling functionality. If you want to scroll something you need ScrollViewer
control
<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>
这篇关于WPF Grid不显示滚动条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:WPF Grid不显示滚动条


基础教程推荐
- 全局 ASAX - 获取服务器名称 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01