如何将网格停靠在顶部?

时间:2018-08-13 22:15:36

标签: c# .net wpf dockpanel

在WPF中,如何将网格停靠在顶部?我在Grid中有一个ItemsControlDockPanel。第一个网格用作列标题,当我滚动ItemsControl的内容时,我不希望它们滚动。我尝试设置DockPanel.Dock="Top",但这并没有使网格在顶部粘住/冻结。

下面是我的情况的最小/简单副本。

XAML:

<Window x:Class="SimplerOne.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:SimplerOne"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800">
<Window.Resources>
    <CollectionViewSource x:Key="years" Source="{Binding Years}" />
</Window.Resources>
<Grid>
    <ScrollViewer HorizontalScrollBarVisibility="Auto">
        <DockPanel LastChildFill="True">
            <Grid DockPanel.Dock="Top">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition></ColumnDefinition>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition></RowDefinition>
                </Grid.RowDefinitions>
                <!-- i have some items control that expand horizontally for col headers here. I want it to stick to the top.-->
                <TextBlock Text="headers"></TextBlock>
            </Grid>

            <ItemsControl x:Name="icContents" ItemsSource="{Binding Source={StaticResource years}}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <TextBox Text="{Binding Year}"></TextBox>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </DockPanel>
    </ScrollViewer>
</Grid>

在后面的代码中:

public partial class MainWindow : Window
{
    private ObservableCollection<RunYear> _years = new ObservableCollection<RunYear>();
    public ObservableCollection<RunYear> Years { get{return _years; } }
    public MainWindow()
    {
        DataContext = this;
        InitializeComponent();
        GenerateData();
    }

    private void GenerateData()
    {
        for (int i = 2010; i < 2075; i++)
        {

           Years.Add(new RunYear(i));
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您的第二个GridScrollViewer内部,因此显然可以滚动。你应该把它放在外面。

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <Grid Grid.Row="0">
        <Grid.ColumnDefinitions>
            <ColumnDefinition></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <!-- i have some items control that expand horizontally for col headers here. I want it to stick to the top.-->
        <TextBlock Text="headers"></TextBlock>
    </Grid>
    <ScrollViewer Grid.Row="1" HorizontalScrollBarVisibility="Auto">
        <DockPanel LastChildFill="True">
            <ItemsControl x:Name="icContents" ItemsSource="{Binding Source={StaticResource years}}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <TextBox Text="{Binding Year}"></TextBox>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </DockPanel>
    </ScrollViewer>
</Grid>

编辑

选项2:

<Grid>
    <DockPanel LastChildFill="True">
        <Grid DockPanel.Dock="Top">
            <Grid.ColumnDefinitions>
                <ColumnDefinition></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition></RowDefinition>
            </Grid.RowDefinitions>
            <!-- i have some items control that expand horizontally for col headers here. I want it to stick to the top.-->
            <TextBlock Text="headers"></TextBlock>
        </Grid>
        <ScrollViewer HorizontalScrollBarVisibility="Auto">

            <ItemsControl x:Name="icContents" ItemsSource="{Binding Source={StaticResource years}}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <TextBox Text="{Binding Year}"></TextBox>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </ScrollViewer>
    </DockPanel>
</Grid>