我想知道在.net 4.0中的WPF数据网格中是否可以有一个静态行。
我想要实现的是创建一个静态行(第0行),当数据网格向下滚动时,它将始终显示在顶部。
当用户滚动数据网格时,想法是第0行将始终在视图中。
谢谢。
答案 0 :(得分:6)
这个“简单的解决方案”仅适用于可冻结的页脚,冻结的标题解决方案会有所不同(实际上更容易 - 只需使用HeaderTeamplate - 将堆叠面板与所需的多个项目堆叠起来) 。
所以我需要一个可以冻结的页脚行,我几个月都找不到任何东西,所以最后我决定不再懒惰并进行调查。
因此,如果你需要一个页脚,那么要点就是在行之间的DataGrid模板和水平滚动查看器中找到一个位置,在那里你可以使用带有单元格的ItemsControl来挤压额外的Grid.Row。
攻击计划:
首先,提取DataGrid模板(我使用了Blend)。熟悉模板后,请按顺序记下部件:
PART_ColumnHeadersPresenter
PART_ScrollContentPresenter
PART_VerticalScrollBar
在PART_VerticalScrollBar下面,有一个网格(为了清楚起见,我会在这里发布)
<Grid Grid.Column="1" Grid.Row="2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="{Binding NonFrozenColumnsViewportHorizontalOffset, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<ScrollBar x:Name="PART_HorizontalScrollBar" Grid.Column="1" Maximum="{TemplateBinding ScrollableWidth}" Orientation="Horizontal" Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}" Value="{Binding HorizontalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" ViewportSize="{TemplateBinding ViewportWidth}"/>
</Grid>
这是我修改的网格,包括“freezable / footer row”。我将简单地对颜色进行硬编码,并将Binding替换为具有良好帮助的“假装”属性以简化(我将它们标记为“MyViewModel.SomeProperty,因此它们很容易看到):
<Grid Grid.Column="1" Grid.Row="2" x:Name="PART_DataGridColumnsVisualSpace">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Path=NonFrozenColumnsViewportHorizontalOffset}"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<ScrollBar Grid.Column="2" Grid.Row="3" Name="PART_HorizontalScrollBar" Orientation="Horizontal"
Maximum="{TemplateBinding ScrollableWidth}" ViewportSize="{TemplateBinding ViewportWidth}"
Value="{Binding Path=HorizontalOffset, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}"
Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}"/>
<Border x:Name="PART_FooterRowHeader" Grid.Row="1" Height="30" Background="Gray" BorderBrush="Black" BorderThickness="0.5">
<TextBlock Margin="4,0,0,0" VerticalAlignment="Center">MY FOOTER</TextBlock>
</Border>
<ItemsControl x:Name="PART_Footer" ItemsSource="{Binding MyViewModel.FooterRow}"
Grid.Row="1" Grid.Column="1" Height="30">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border Background="Gray" BorderThickness="0,0,0.5,0.5" BorderBrush="Black">
<!-- sticking a textblock as example, i have a much more complex control here-->
<TextBlock Text="{Binding FooterItemValue}"/>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.Template>
<ControlTemplate>
<ScrollViewer x:Name="PART_Footer_ScrollViewer" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"
CanContentScroll="True" Focusable="false">
<StackPanel IsItemsHost="True" Orientation="Horizontal"/>
</ScrollViewer>
</ControlTemplate>
</ItemsControl.Template>
</ItemsControl>
</Grid>
还添加到DataGrid响应滚动和标题调整大小
<DataGrid ... ScrollViewer.ScrollChanged="OnDatagridScrollChanged"
<Style TargetType="DataGridColumnHeader">
<EventSetter Event="SizeChanged" Handler="OnDataColumnSizeChanged"/>
</Style>
现在,回到.xaml.cs
基本上需要两件事:
(1)同步列调整大小(以便相应的页脚单元格调整大小) (2)使用页脚滚动同步DataGrid滚动
//syncs the footer with column header resize
private void OnDatagridScrollChanged(object sender, ScrollChangedEventArgs e)
{
if (e.HorizontalChange == 0.0) return;
FooterScrollViewer.ScrollToHorizontalOffset(e.HorizontalOffset);
}
//syncs scroll
private void OnDataColumnSizeChanged(object sender, SizeChangedEventArgs e)
{
//I don't know how many of these checks you need, skip if need to the gist
if (!_isMouseDown) return;
if (!_dataGridLoaded) return;
if (!IsVisible) return;
var header = (DataGridColumnHeader)sender;
var index = header.DisplayIndex - ViewModel.NumberOfHeaderColumns;
if (index < 0 || index >= FooterCells.Count) return;
FooterCells[index].Width = e.NewSize.Width;
}
//below referencing supporting properties:
private ScrollViewer _footerScroll;
private ScrollViewer FooterScrollViewer
{
get {
return _footerScroll ??
(_footerScroll = myDataGrid.FindVisualChildByName<ScrollViewer>("PART_Footer_ScrollViewer"));
}
}
//added this so I don't have to hunt them down from XAML every time
private List<Border> _footerCells;
private List<Border> FooterCells
{
get
{
if (_footerCells == null)
{
var ic = myDataGrid.FindVisualChildByName<ItemsControl>("PART_Footer");
_footerCells = new List<Border>();
for (var i = 0; i < ic.Items.Count; i++)
{
var container = ic.ItemContainerGenerator.ContainerFromIndex(i);
var border = ((Visual)container).FindVisualChild<Border>();
_footerCells.Add(border);
}
}
return _footerCells;
}
}
就是这样!我认为最重要的部分是XAML,看看你可以把你的“freezable row”放在哪里,其他一切,比如操纵/同步东西都很容易 - 几乎是一个衬里)
答案 1 :(得分:0)
我不确定行,但您可以使用 FrozenColumnCount 冻结列。这样它总是可见的。应该有冻结属性。