打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
How to write a Scrollable Data Grid in WPF?
How to write a Scrollable Data Grid in WPF?

 

Ifyou want to display a large number of rows in a grid you willdefinitely need a scroll feature in your grid but does the grid controlin WPF has this feature? The answer is pretty straight, NO.

 

Hmm,then how I am supposed to display 100 records in a grid? Well the firstanswer to that is just use the ScrollViewer control around the grid.That would be a short time solution, once run your app you will come toagreement that is not really what you want.  Youwill notice that the header part of the grid will get included in theScrollViewer as well, which is not very pleasant to the user basicallywhen the user scrolls down, the header gets disappear.

 

Isthere any other solution? Yes, there is a nicer way. It is just notvery obvious at first but you will get used to that after using it forcouple times. Here are the steps: First of all you need to have twogrids one for the header and the other one for the main body. The maingrid will be in the ScrollViewer control as you would also guess. Sothis will solve the problem, very simple!!!

 

<Grid Name="grid1"  TextBlock.Foreground="Chocolate">

   <Grid.Resources>

      <Style  TargetType="{x:Type Border}">

            <Setter Property="BorderThickness" Value="1" />

            <Setter Property="Background" Value="AntiqueWhite" />

            <Setter Property="BorderBrush" Value="Gray" />

      </Style>                          

   </Grid.Resources>

   <Grid.ColumnDefinitions>

      <ColumnDefinition Width="0.2*"/>

      <ColumnDefinition Width="0.2*"/>

      <ColumnDefinition Width="0.2*"/>

      <ColumnDefinition Width="0.2*"/>

   </Grid.ColumnDefinitions>

   <Border Grid.Column="0" >

      <TextBlock>Role</TextBlock>

   </Border>

   <Border Grid.Column="1"  >

      <TextBlock>Name</TextBlock>

   </Border>

   <Border Grid.Column="2"  >

      <TextBlock>Start Date</TextBlock>

   </Border>

   <Border Grid.Column="3"  >

      <TextBlock>End Date</TextBlock>

   </Border>

</Grid>

<ScrollViewer Height="100" HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Visible" >

  <Grid Name="grid2"  >

    <Grid.ColumnDefinitions>

      <ColumnDefinition Width="0.08*"/>

      <ColumnDefinition Width="0.1*"/>

      <ColumnDefinition Width="0.14*"/>

      <ColumnDefinition Width="0.14*"/>

    </Grid.ColumnDefinitions>

    <Grid.RowDefinitions>

      <RowDefinition Height="0.2*" />

      <RowDefinition Height="0.2*" />

      <RowDefinition Height="0.2*" />

      <RowDefinition Height="0.2*" />

      <RowDefinition Height="0.2*" />

    </Grid.RowDefinitions>

      <TextBlock Grid.Row="0" Grid.Column="0">Role1</TextBlock>

      <TextBlock Grid.Row="0" Grid.Column="1">

            <Hyperlink NavigateUri="Page1.xaml">Name1</Hyperlink>

      </TextBlock>

      <TextBlock Grid.Row="0" Grid.Column="2">01/01/2006</TextBlock>

      <TextBlock Grid.Row="0" Grid.Column="3">01/01/2007</TextBlock>

      <TextBlock Grid.Row="1" Grid.Column="0">Role2</TextBlock>

      <TextBlock Grid.Row="1" Grid.Column="1">

            <Hyperlink NavigateUri="Page2.xaml">Name2</Hyperlink>

      </TextBlock>

      <TextBlock Grid.Row="1" Grid.Column="2">01/01/2007</TextBlock>

      <TextBlock Grid.Row="1" Grid.Column="3">01/01/2008</TextBlock>

      <TextBlock Grid.Row="2" Grid.Column="0">Role3</TextBlock>

      <TextBlock Grid.Row="2" Grid.Column="1">

            <Hyperlink NavigateUri="Page3.xaml">Name3</Hyperlink>

      </TextBlock>

      <TextBlock Grid.Row="2" Grid.Column="2">01/01/2006</TextBlock>

      <TextBlock Grid.Row="2" Grid.Column="3">01/01/2007</TextBlock>

      <TextBlock Grid.Row="3" Grid.Column="0">Role4</TextBlock>

      <TextBlock Grid.Row="3" Grid.Column="1">

            <Hyperlink NavigateUri="Page4.xaml">Name4</Hyperlink>

      </TextBlock>

      <TextBlock Grid.Row="3" Grid.Column="2">01/01/2007</TextBlock>

      <TextBlock Grid.Row="3" Grid.Column="3">01/01/2008</TextBlock>

      <TextBlock Grid.Row="4"  Grid.Column="0">Role5</TextBlock>

      <TextBlock Grid.Row="4" Grid.Column="1">

            <Hyperlink NavigateUri="Page5.xaml">Name5</Hyperlink>

      </TextBlock>

      <TextBlock Grid.Row="4" Grid.Column="2">01/01/2006</TextBlock>

      <TextBlock Grid.Row="4" Grid.Column="3">01/01/2007</TextBlock>

   </Grid>

</ScrollViewer>

 

Butnow there is another issue that is the size of the header columnsdoesn’t match the size of the main grid columns. To fix this you justneed to simply use this grid property SharedSizeGroup. Set thisproperty on every column on the header grid to the unique name and thenset the use the same names to set this property on the main gridcolumns. That simple!!!

 

    <Grid.ColumnDefinitions>

      <ColumnDefinition Width="0.08*" SharedSizeGroup="Role"/>

      <ColumnDefinition Width="0.1*" SharedSizeGroup="Name"/>

      <ColumnDefinition Width="0.14*" SharedSizeGroup="StartDate"/>

      <ColumnDefinition Width="0.14*" SharedSizeGroup="EndDate"/>

    </Grid.ColumnDefinitions>

 

Onelast thing that you will notice is that the size of the last column onthe header does not match the last column on the main grid. Hmm, nowyou need to this last trick to fix this issue. First you need to addone more ColumnDefinition to your header grid by setting its width toAuto:

 

<ColumnDefinition Width="Auto" />

 

Then add this item to be the last column in the header grid and set the width to this DynamicResource.

 

   <FrameworkElement Grid.Column="4" Width="{DynamicResource {x:Static SystemParameters.ScrollWidthKey}}" />

 

Done! You have built the pretty nice looking grid that can scroll nicely.
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
WPF笔记(2.4 Grid)
C# WPF从后台代码生成行列可变的表格
迁移桌面程序到MS Store(11)——应用SVG图标
Expression Blend实例中文教程(3)
Windows Phone新手开发教程(二)
C# WPF计算器界面(Calculator Design With Animations)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服