如何在wpf datagrid中添加页脚行?我不得不在wpf数据网格中添加一行以获得每列的总和,我不想使用任何dll或telerik以及类似的东西只使用微软组件来执行此操作.我试着这样做:
<Style TargetType="{x:Type DataGrid}"> <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" /> <Setter Property="BorderBrush"> <Setter.Value> <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> <GradientStop Color="{DynamicResource BorderLightColor}" Offset="0" /> <GradientStop Color="{DynamicResource BorderDarkColor}" Offset="1" /> </LinearGradientBrush> </Setter.Value> </Setter> <Setter Property="BorderThickness" Value="1" /> <Setter Property="RowDetailsVisibilityMode" Value="VisibleWhenSelected" /> <Setter Property="ScrollViewer.CanContentScroll" Value="true" /> <Setter Property="ScrollViewer.PanningMode" Value="Both" /> <Setter Property="Stylus.IsFlicksEnabled" Value="False" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type DataGrid}"> <Border x:Name="border" SnapsToDevicePixels="True" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}"> <Border.Background> <SolidColorBrush Color="{DynamicResource ControlLightColor}" /> </Border.Background> <VisualStateManager.VisualStateGroups> <VisualStateGroup x:Name="CommonStates"> <VisualState x:Name="Disabled"> <Storyboard> <ColorAnimationUsingKeyFrames Storyboard.TargetName="border" Storyboard.TargetProperty="(Panel.Background). (SolidColorBrush.Color)"> <EasingColorKeyFrame KeyTime="0" Value="{DynamicResource ControlLightColor}" /> </ColorAnimationUsingKeyFrames> </Storyboard> </VisualState> <VisualState x:Name="Normal" /> </VisualStateGroup> </VisualStateManager.VisualStateGroups> <ScrollViewer x:Name="DG_ScrollViewer" Focusable="false" Background="Black"> <ScrollViewer.Template> <ControlTemplate TargetType="{x:Type ScrollViewer}"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="*" /> <ColumnDefinition Width="Auto" /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="*" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <Button Focusable="false" Command="{x:Static DataGrid.SelectAllCommand}" Style="{DynamicResource {ComponentResourceKey ResourceId=DataGridSelectAllButtonStyle,TypeInTargetAssembly={x:Type DataGrid}}}" Visibility="{Binding HeadersVisibility,ConverterParameter={x:Static DataGridHeadersVisibility.All},Converter={x:Static DataGrid.HeadersVisibilityConverter},RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" Width="{Binding CellsPanelHorizontalOffset,RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" /> <DataGridColumnHeadersPresenter x:Name="PART_ColumnHeadersPresenter" Grid.Column="1" Visibility="{Binding HeadersVisibility,ConverterParameter={x:Static DataGridHeadersVisibility.Column},RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" /> <ScrollContentPresenter x:Name="PART_ScrollContentPresenter" Grid.ColumnSpan="2" Grid.Row="1" CanContentScroll="{TemplateBinding CanContentScroll}" /> <ScrollBar x:Name="PART_VerticalScrollBar" Grid.Column="2" Grid.Row="1" Orientation="Vertical" ViewportSize="{TemplateBinding ViewportHeight}" Maximum="{TemplateBinding ScrollableHeight}" Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}" Value="{Binding VerticalOffset,Mode=OneWay,RelativeSource={RelativeSource TemplatedParent}}"/> <TextBlock Grid.Row="2" Grid.Column="1" Text="This is footer!"/> <Grid Grid.Column="1" Grid.Row="3"> <Grid.ColumnDefinitions> <ColumnDefinition Width="{Binding NonFrozenColumnsViewportHorizontalOffset,RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <ScrollBar x:Name="PART_HorizontalScrollBar" Grid.Column="1" Orientation="Horizontal" ViewportSize="{TemplateBinding ViewportWidth}" Maximum="{TemplateBinding ScrollableWidth}" Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}" Value="{Binding HorizontalOffset,RelativeSource={RelativeSource TemplatedParent}}"/> </Grid> </Grid> </ControlTemplate> </ScrollViewer.Template> <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" /> </ScrollViewer> </Border> </ControlTemplate> </Setter.Value> </Setter> <Style.Triggers> <Trigger Property="IsGrouping" Value="true"> <Setter Property="ScrollViewer.CanContentScroll" Value="false" /> </Trigger> </Style.Triggers> </Style>
我还尝试添加网格而不是文本块,但在调整datagrid列时,它们无法调整大小并且看起来非常难看.
解决方法
试试这个.
<ScrollViewer VerticalAlignment="Top" HorizontalScrollBarVisibility="Auto"> <StackPanel> <DataGrid ScrollViewer.HorizontalScrollBarVisibility="Disabled"> <DataGrid.Columns> <DataGridTextColumn x:Name="Header1" Header="Header1"></DataGridTextColumn> <DataGridTextColumn x:Name="Header2" Header="Header2"></DataGridTextColumn> <DataGridTextColumn x:Name="Header3" Header="Header3"></DataGridTextColumn> </DataGrid.Columns> <TextBlock></TextBlock> <TextBlock></TextBlock> </DataGrid> <StackPanel HorizontalAlignment="Left" Orientation="Horizontal"> <Grid Width="{Binding ElementName=Header1,Path=ActualWidth}"> <TextBlock Margin="5,0" Text="Footer1"></TextBlock> </Grid> <Grid Width="{Binding ElementName=Header2,Path=ActualWidth}"> <TextBlock Text="Footer2" Margin="5,0"></TextBlock> </Grid> <Grid Width="{Binding ElementName=Header3,Path=ActualWidth}"> <TextBlock Text="Footer3" Margin="5,0"></TextBlock> </Grid> </StackPanel> </StackPanel> </ScrollViewer>
结果