首先添加 列的样式引用
xmlns:dgCloumn="clr-namespace:System.windows.Controls.Primitives;assembly=System.windows.Controls.Data"
自定义多表头样式
<UserControl.Resources> <Style x:Key="DZFXStyle" targettype="dgCloumn:DataGrIDColumnheader"> <Setter Property="Foreground" Value="#222"/> <Setter Property="HorizontalContentAlignment" Value="Stretch"/> <Setter Property="VerticalContentAlignment" Value="Stretch"/> <Setter Property="IsTabStop" Value="False"/> <Setter Property="SeparatorBrush" Value="#ccc"/> <Setter Property="padding" Value="8"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate> <GrID x:name="Root"> <GrID.ColumnDeFinitions> <ColumnDeFinition WIDth="1"/> <ColumnDeFinition WIDth="*"/> <ColumnDeFinition WIDth="1"/> </GrID.ColumnDeFinitions> <Rectangle x:name="VerticalSeparator1" Fill="#ccc" VerticalAlignment="Stretch" WIDth="1" Visibility="Visible" GrID.Column="0" GrID.Row="0" /> <Rectangle x:name="BackgroundGradIEnt" Fill="#eee" Stretch="Fill" GrID.Column="1" GrID.ColumnSpan="2"> </Rectangle> <GrID HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" GrID.Column="1" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"> <GrID.RowDeFinitions> <RowDeFinition Height="20*" /> <RowDeFinition Height="1" /> <RowDeFinition Height="20*" /> </GrID.RowDeFinitions> <GrID.ColumnDeFinitions> <ColumnDeFinition WIDth="100*"/> <ColumnDeFinition WIDth="1" /> <ColumnDeFinition WIDth="100*"/> <ColumnDeFinition WIDth="1" /> <ColumnDeFinition WIDth="100*"/> <ColumnDeFinition WIDth="1" /> <ColumnDeFinition WIDth="100*"/> </GrID.ColumnDeFinitions> <ContentPresenter Content="土壤墒情(%)" VerticalAlignment="Stretch" HorizontalAlignment="Center" GrID.ColumnSpan="8" GrID.Column="0" /> <Rectangle Fill="#ccc" VerticalAlignment="Stretch" Height="1" Visibility="Visible" GrID.Row="1" GrID.ColumnSpan="7" /> <ContentPresenter Content="10CM" GrID.Row="2" VerticalAlignment="Stretch" HorizontalAlignment="Center" /> <Rectangle Fill="#ccc" VerticalAlignment="Stretch" WIDth="1" Visibility="Visible" GrID.Row="2" GrID.Column="1" /> <ContentPresenter Content="20CM" GrID.Row="2" GrID.Column="2" VerticalAlignment="Stretch" HorizontalAlignment="Center" /> <Rectangle Fill="#ccc" VerticalAlignment="Stretch" WIDth="1" Visibility="Visible" GrID.Row="2" GrID.Column="3" /> <ContentPresenter Content="40CM" GrID.Row="2" GrID.Column="4" VerticalAlignment="Stretch" HorizontalAlignment="Center" /> <Rectangle Fill="#ccc" VerticalAlignment="Stretch" WIDth="1" Visibility="Visible" GrID.Row="2" GrID.Column="5" /> <ContentPresenter Content="60CM" GrID.Row="2" GrID.Column="6" VerticalAlignment="Stretch" HorizontalAlignment="Center" /> </GrID> <Rectangle x:name="VerticalSeparator" Fill="#ccc" VerticalAlignment="Stretch" WIDth="1" Visibility="Visible" GrID.Row="1" GrID.Column="2"/> </GrID> </ControlTemplate> </Setter.Value> </Setter> </Style> </UserControl.Resources>
DataGrID 控件 创建
<sdk:DataGrID autoGenerateColumns="False" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ItemsSource="{Binding}" GrID.Row="1" name="dataGrID1"> <sdk:DataGrID.Columns> <sdk:DataGrIDTemplateColumn headerStyle="{StaticResource DZFXStyle}" WIDth="200*"> <sdk:DataGrIDTemplateColumn.CellTemplate> <DataTemplate> <GrID> <GrID.ColumnDeFinitions> <ColumnDeFinition WIDth="100*" /> <ColumnDeFinition WIDth="1" /> <ColumnDeFinition WIDth="100*" /> <ColumnDeFinition WIDth="1" /> <ColumnDeFinition WIDth="100*"/> <ColumnDeFinition WIDth="1" /> <ColumnDeFinition WIDth="100*"/> </GrID.ColumnDeFinitions> <TextBlock Text="{Binding SLM10}" HorizontalAlignment="Stretch" VerticalAlignment="Center" GrID.Column="0" /> <Rectangle Fill="#ccc" VerticalAlignment="Stretch" WIDth="1" GrID.Column="1"/> <sdk:Label Content="{Binding SLM20}" HorizontalAlignment="Stretch" VerticalAlignment="Center" GrID.Column="2"/> <Rectangle Fill="#ccc" VerticalAlignment="Stretch" WIDth="1" GrID.Column="3" /> <sdk:Label Content="{Binding SLM40}" HorizontalAlignment="Stretch" VerticalAlignment="Center" GrID.Column="4"/> <Rectangle Fill="#ccc" VerticalAlignment="Stretch" WIDth="1" GrID.Column="5"/> <sdk:Label Content="{Binding SLM60}" HorizontalAlignment="Stretch" VerticalAlignment="Center" GrID.Column="6" /> </GrID> </DataTemplate> </sdk:DataGrIDTemplateColumn.CellTemplate> </sdk:DataGrIDTemplateColumn> </sdk:DataGrID.Columns> </sdk:DataGrID>创建一个模板列 里面放一个GrID(主要控制每一列的宽度)
GrID 必须与自定义样式 里的GrID 每列是一一对应的 否则显示的数据将会错位
实际上就是在表头里定义一个GrID 去定义多表头的样式
在DataGrID 定义一个与之对就的GrID去绑定具体的数值
PS:定义GrID样式头时 边框其实就是一个Rectangle 宽度为1 或者长度为1 它也是要占单元格的
总结以上是内存溢出为你收集整理的Silverlight DataGrid 多表头 样式实现全部内容,希望文章能够帮你解决Silverlight DataGrid 多表头 样式实现所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)