如何将网格停靠在顶部?
How to dock a grid to the top?
在 WPF 中,如何将网格停靠到顶部?我在 DockPanel
中有一个 Grid
和 ItemsControl
。第一个网格用作 headers 列,我不希望它们在我滚动浏览 ItemsControl
的内容时滚动。我尝试设置 DockPanel.Dock="Top"
,但未能将网格设置为 stick/freeze。
下面是我的情况的minimal/simpler副本。
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));
}
}
}
您的第二个 Grid
在 ScrollViewer
内,因此它显然会滚动。你应该把它放在外面。
<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>
在 WPF 中,如何将网格停靠到顶部?我在 DockPanel
中有一个 Grid
和 ItemsControl
。第一个网格用作 headers 列,我不希望它们在我滚动浏览 ItemsControl
的内容时滚动。我尝试设置 DockPanel.Dock="Top"
,但未能将网格设置为 stick/freeze。
下面是我的情况的minimal/simpler副本。
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));
}
}
}
您的第二个 Grid
在 ScrollViewer
内,因此它显然会滚动。你应该把它放在外面。
<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>