[英]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.