[英]ScrollViewer does not work with grid in wpf
我想在网格上有一个垂直滚动条,这看起来很简单,但是由于某种原因它根本无法工作。 当我将VerticalScrollBarVisibility设置为visible时,它会显示,但不执行任何操作。 设置为自动时,它根本不会显示。
我已经阅读了该网站上的建议,但似乎对我不起作用。 我知道行应设置为固定高度或*,并且我将两者结合在一起。 我还尝试按照建议的那样设置网格的最大高度和滚动条的高度,但是这些都不起作用。
这就是我的设置方法(网格位于选项卡中):
</TabItem.Header>
<ScrollViewer VerticalScrollBarVisibility="Auto">
<Grid Name="CSGrid">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="400"/>
<ColumnDefinition Width="200"/>
<ColumnDefinition Width="200"/>
<ColumnDefinition Width="200"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="20"/>
<RowDefinition Height="5"/>
<RowDefinition Height="1"/>
<RowDefinition Height="20"/>
<RowDefinition Height="20"/>
<RowDefinition Height="20"/>
...
然后,我会有大量的行,它们的内容是通过C#代码设置的(如果有区别的话)。 所有的高度都设置为20。此后,我在网格中也有许多矩形和文本块,我想不到的没有问题-除非它们以某种方式有所作为?
在代码中,我将文本添加到这样的行中:
TextBlock hist1 = new TextBlock();
TextBlock hist2 = new TextBlock();
TextBlock hist3 = new TextBlock();
TextBlock hist4 = new TextBlock();
TextBlock hist5 = new TextBlock();
string [] allHist = File.ReadAllLines(“ MedicalHistory.txt”);
hist1.Text = allHist[0];
hist2.Text = allHist[1];
hist3.Text = allHist[2];
hist4.Text = allHist[3];
hist5.Text = allHist[4];
CSGrid.Children.Add(hist1);
CSGrid.Children.Add(hist2);
CSGrid.Children.Add(hist3);
CSGrid.Children.Add(hist4);
CSGrid.Children.Add(hist5);
Grid.SetColumn(hist1, 0);
Grid.SetColumn(hist2, 0);
Grid.SetColumn(hist3, 0);
Grid.SetColumn(hist4, 0);
Grid.SetColumn(hist5, 0);
Grid.SetRow(hist1, 5);
Grid.SetRow(hist2, 6);
Grid.SetRow(hist3, 7);
Grid.SetRow(hist4, 8);
Grid.SetRow(hist5, 9);
任何帮助将不胜感激
在网格中使用ScrollViewer.CanContentScroll =“ True”:
<ScrollViewer VerticalScrollBarVisibility="Auto">
<Grid Name="CSGrid" ScrollViewer.CanContentScroll="True">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="400"/>
<ColumnDefinition Width="200"/>
<ColumnDefinition Width="200"/>
<ColumnDefinition Width="200"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="20"/>
<RowDefinition Height="5"/>
<RowDefinition Height="1"/>
<RowDefinition Height="20"/>
<RowDefinition Height="20"/>
<RowDefinition Height="20"/>
...
您应将RowDefinition
Height
属性设置为大于TabControl
Height。
我为您编写了代码,并且效果很好:
<Window x:Class="TestApp13.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:src="clr-namespace:TestApp13"
Title ="Title" Height="600" Width="800">
<TabControl>
<TabItem Header="Tab 1">
<ScrollViewer VerticalScrollBarVisibility="Auto">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="200"/>
<RowDefinition Height="200"/>
<RowDefinition Height="200"/>
<RowDefinition Height="200"/>
<RowDefinition Height="200"/>
<RowDefinition Height="2000"/>
</Grid.RowDefinitions>
<Button Width="100" Height="30" Grid.Row="0"/>
<Button Width="100" Height="30" Grid.Row="1"/>
<Button Width="100" Height="30" Grid.Row="2"/>
<Button Width="100" Height="30" Grid.Row="3"/>
<Button Width="100" Height="30" Grid.Row="4"/>
<Button Width="100" Height="30" Grid.Row="5"/>
</Grid>
</ScrollViewer>
</TabItem>
</TabControl>
<Window x:Class="TestApp13.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:src="clr-namespace:TestApp13"
Title ="Title" Height="600" Width="800">
<TabControl>
<TabItem Header="Tab 1">
<ScrollViewer Hight="500">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="200"/>
<RowDefinition Height="200"/>
<RowDefinition Height="200"/>
<RowDefinition Height="200"/>
<RowDefinition Height="200"/>
<RowDefinition Height="2000"/>
</Grid.RowDefinitions>
<Button Width="100" Height="30" Grid.Row="0"/>
<Button Width="100" Height="30" Grid.Row="1"/>
<Button Width="100" Height="30" Grid.Row="2"/>
<Button Width="100" Height="30" Grid.Row="3"/>
<Button Width="100" Height="30" Grid.Row="4"/>
<Button Width="100" Height="30" Grid.Row="5"/>
</Grid>
</ScrollViewer>
</TabItem>
</TabControl>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.