Customizing Silverlight 3 DataGrid Headers

Customizing Silverlight 3 DataGrid Headers,第1张

概述from :http://weblogs.asp.net/dwahlin/archive/2009/06/11/customizing-silverlight-3-datagrid-headers.aspx We’re currently working on a client application that captures time sheet information and needed

from :http://weblogs.asp.net/dwahlin/archive/2009/06/11/customizing-silverlight-3-datagrid-headers.aspx

We’re currently working on a clIEnt application that captures time sheet information and needed the ability to completely customize the look and feel of DataGrID headers.  Fortunately,that’s fairly straightforward to do in Silverlight 3 (or Silverlight 2 for that matter).  Nearly all of the columns being used are DataGrIDTemplateColumn types (although that’s not required to customize headers) and changing the header is accomplished by using the headerStyle property as shown next: <data:DataGrIDTemplateColumn header="Tue" headerStyle="{StaticResource TimeSheetDayheaderStyle}">     <data:DataGrIDTemplateColumn.CellTemplate>         <DataTemplate>             <StackPanel OrIEntation="Horizontal">                 <TextBox Text="{Binding TuesdayQuantity}" Style="{StaticResource TimeSheetTextBoxStyle}"/>                 <Rectangle Fill="#FFC9CACA" VerticalAlignment="Stretch" WIDth="1" />                 <TextBox Text="{Binding TuesdayHours}" margin="2,0" Style="{StaticResource TimeSheetTextBoxStyle}"/>             </StackPanel>         </DataTemplate>     </data:DataGrIDTemplateColumn.CellTemplate> </data:DataGrIDTemplateColumn> The tricky part of creating a custom header for the first time is kNowing what the style’s targettype should be.  You’ll need to reference the System.windows.Controls.Primitives namespace as shown next as it contains the DataGrIDColumnheader type:  xmlns:dataprimitives="clr-namespace:System.windows.Controls.Primitives;assembly=System.windows.Controls.Data" Once that’s available you can define the styles.  We needed stacked headers that displayed each day of the week as well as hours and quantity under the day and ended up going with the following styles (note that Silverlight 3’s new BasedOn feature is being used).  The TimeSheetDayheaderStyle style defines the stacked headers using a GrID control.  <Style x:Key="DataGrIDBaseheaderStyle" targettype="dataprimitives:DataGrIDColumnheader">     <Setter Property="FontWeight" Value="Bold" /> </Style> <Style x:Key="TimeSheetTotalsheaderStyle" targettype="dataprimitives:DataGrIDColumnheader"        BasedOn="{StaticResource TimeSheetDayheaderStyle}">     <Setter Property="Foreground" Value="#FFFF0000"/> </Style> <Style x:Key="TimeSheetDayheaderStyle" targettype="dataprimitives:DataGrIDColumnheader"         BasedOn="{StaticResource DataGrIDBaseheaderStyle}">     <Setter Property="Foreground" Value="#FF000000"/>     <Setter Property="HorizontalContentAlignment" Value="left"/>     <Setter Property="VerticalContentAlignment" Value="Center"/>     <Setter Property="IsTabStop" Value="False"/>     <Setter Property="SeparatorBrush" Value="#FFC9CACA"/>     <Setter Property="padding" Value="8"/>     <Setter Property="Template">         <Setter.Value>             <ControlTemplate>                 <GrID x:name="Root">                     <GrID.ColumnDeFinitions>                         <ColumnDeFinition/>                         <ColumnDeFinition WIDth="auto"/>                     </GrID.ColumnDeFinitions>                     <visualstatemanager.VisualStateGroups>                         <VisualStateGroup x:name="CommonStates">                             <VisualState x:name="normal"/>                             <VisualState x:name="MouSEOver">                                 <Storyboard>                                     <colorAnimation Duration="0"                                                      Storyboard.Targetname="BackgroundRectangle"                                                      Storyboard.TargetProperty="(Fill).color" To="#FF448DCA"/>                                     <colorAnimation Duration="0"                                                      Storyboard.Targetname="BackgroundGradIEnt"                                                      Storyboard.TargetProperty="(Fill).(GradIEntStops)[3].color" To="#7FFFFFFF"/>                                     <colorAnimation Duration="0"                                                      Storyboard.Targetname="BackgroundGradIEnt"                                                      Storyboard.TargetProperty="(Fill).(GradIEntStops)[2].color" To="#CCFFFFFF"/>                                     <colorAnimation Duration="0"                                                      Storyboard.Targetname="BackgroundGradIEnt"                                                      Storyboard.TargetProperty="(Fill).(GradIEntStops)[1].color" To="#F2FFFFFF"/>                                 </Storyboard>                             </VisualState>                             <VisualState x:name="pressed">                                 <Storyboard>                                     <colorAnimation Duration="0"                                                      Storyboard.Targetname="BackgroundRectangle"                                                      Storyboard.TargetProperty="(Fill).color" To="#FF448DCA"/>                                     <colorAnimation Duration="0"                                                      Storyboard.Targetname="BackgroundGradIEnt"                                                      Storyboard.TargetProperty="(Fill).(GradIEntStops)[0].color" To="#D8FFFFFF"/>                                     <colorAnimation Duration="0"                                                      Storyboard.Targetname="BackgroundGradIEnt"                                                      Storyboard.TargetProperty="(Fill).(GradIEntStops)[1].color" To="#C6FFFFFF"/>                                     <colorAnimation Duration="0"                                                      Storyboard.Targetname="BackgroundGradIEnt"                                                      Storyboard.TargetProperty="(Fill).(GradIEntStops)[2].color" To="#8CFFFFFF"/>                                     <colorAnimation Duration="0"                                                      Storyboard.Targetname="BackgroundGradIEnt"                                                      Storyboard.TargetProperty="(Fill).(GradIEntStops)[3].color" To="#3FFFFFFF"/>                                 </Storyboard>                             </VisualState>                         </VisualStateGroup>                         <VisualStateGroup x:name="SortStates">                             <VisualState x:name="Unsorted"/>                             <VisualState x:name="SortAscending" />                             <VisualState x:name="SortDescending" />                         </VisualStateGroup>                     </visualstatemanager.VisualStateGroups>                     <Rectangle x:name="BackgroundRectangle" Fill="#FF1F3B53" Stretch="Fill" GrID.ColumnSpan="2"/>                     <Rectangle x:name="BackgroundGradIEnt" Stretch="Fill" GrID.ColumnSpan="2">                         <Rectangle.Fill>                             <linearGradIEntBrush EndPoint=".7,1" StartPoint=".7,0">                                 <GradIEntStop color="#FCFFFFFF" Offset="0.015"/>                                 <GradIEntStop color="#F7FFFFFF" Offset="0.375"/>                                 <GradIEntStop color="#E5FFFFFF" Offset="0.6"/>                                 <GradIEntStop color="#D1FFFFFF" Offset="1"/>                             </linearGradIEntBrush>                         </Rectangle.Fill>                     </Rectangle>                     <GrID HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"                            VerticalAlignment="{TemplateBinding VerticalContentAlignment}">                         <GrID.RowDeFinitions>                             <RowDeFinition Height="20" />                             <RowDeFinition Height="1" />                             <RowDeFinition Height="20" />                         </GrID.RowDeFinitions>                         <GrID.ColumnDeFinitions>                             <ColumnDeFinition WIDth="50"/>                             <ColumnDeFinition WIDth="1" />                              <ColumnDeFinition WIDth="50"/>                         </GrID.ColumnDeFinitions>                         <!-- Row 0 -->                         <ContentPresenter Content="{TemplateBinding Content}"                                            VerticalAlignment="Center" HorizontalAlignment="Center"                                            GrID.ColumnSpan="3" />                                                  <!-- Row 1 -->                         <Rectangle Fill="#FFC9CACA" VerticalAlignment="Stretch" Height="1"                                     Visibility="Visible" GrID.Row="1" GrID.ColumnSpan="3" />                         <!-- Row 2 -->                         <ContentPresenter Content="Qty" GrID.Row="2" VerticalAlignment="Center"                                            HorizontalAlignment="Center" />                         <Rectangle Fill="#FFC9CACA" VerticalAlignment="Stretch" WIDth="1"                                      Visibility="Visible" GrID.Row="2" GrID.Column="1" />                         <ContentPresenter Content="Hours" GrID.Row="2" GrID.Column="2"                                            VerticalAlignment="Center" HorizontalAlignment="Center" />                     </GrID>                     <Rectangle x:name="VerticalSeparator" Fill="#FFC9CACA"                                 VerticalAlignment="Stretch" WIDth="1" Visibility="Visible"                                 GrID.Row="1" GrID.Column="1"/>                 </GrID>             </ControlTemplate>         </Setter.Value>     </Setter> </Style> The end result is a grID with headers that are just like the clIEnt wanted: image    

总结

以上是内存溢出为你收集整理的Customizing Silverlight 3 DataGrid Headers全部内容,希望文章能够帮你解决Customizing Silverlight 3 DataGrid Headers所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1018086.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-23
下一篇2022-05-23

发表评论

登录后才能评论

评论列表(0条)

    保存