【问题标题】:ScrollViewer does not work with grid in wpfScrollViewer 不适用于 wpf 中的网格
【发布时间】:2013-12-07 17:11:41
【问题描述】:

我想在我的网格上有一个垂直滚动条,这看起来很简单,但由于某种原因它不起作用。当我将 VerticalScrollBarVisibility 设置为可见时,它会显示但什么也不做。当它设置为自动时,它根本不显示。

我已阅读此网站上的建议,但它似乎对我不起作用。我知道行应该设置为固定高度或 * 并且我有两者的组合。我还尝试按照建议设置网格的最大高度和滚动条的高度,但这些都不起作用。

这就是我设置它的方式(网格位于选项卡内):

</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。尝试类似 看看它是否出现。
  • 有人告诉我,我通常必须在滚动查看器中设置一个大尺寸,然后为实际的滚动查看器设置所需的尺寸
  • 那是我的错,我已经删除了第二个例子。感谢您的提示。是的,我也是这么读的。我在滚动条中有很多内容,但是当我设置网格的最大高度时,它会被切断,但滚动条仍然没有显示......
  • I then have a large number of rows whose contents I set through C# code - 错误。 不要在 WPF 的过程代码中创建或操作 UI 元素。这就是 XAML 的用途。 - 为此创建一个合适的 ViewModel 并使用 DataBinding 而不是一种骇人听闻的类似 winforms 的程序方法。

标签: c# wpf


【解决方案1】:

在您的网格中使用 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"/>
        ...

【讨论】:

    【解决方案2】:

    你应该设置RowDefinitionHeight属性大于TabControlHeight。

    我为你编写了这个代码并且工作正常:

    <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>
    

    【讨论】:

    • 谢谢,但我无法理解。即使我设置了所有行的高度,它仍然不起作用。我真的很困惑。这可能与我如何将孩子添加到网格中有关吗?我只是在使用 CSGrid.Children.Add(),它似乎工作正常
    • 你能发布 xaml 和 cs 文件吗?因此我可以解决你的问题。
    • 非常感谢,我已经编辑了我的帖子,希望对您有所帮助
    • 你能发布整个 xaml 和 cs 文件吗?
    • 好吧,我终于明白了 - 我必须设置它的高度,我一直专注于网格(网格和滚动查看器在选项卡内): 。感谢您的帮助
    【解决方案3】:
    <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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-16
      • 2012-05-13
      • 1970-01-01
      • 2023-04-04
      • 1970-01-01
      • 2013-07-10
      相关资源
      最近更新 更多