自定义表头Datagrid
自定义的一个表头
1 <bp:BasePage x:Class="NetReform.Pages.RealProbabiTableCompare" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 5 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 6 xmlns:local="clr-namespace:NetReform" 7 xmlns:bp="clr-namespace:NetReform.Pages" 8 mc:Ignorable="d" 9 xmlns:my="clr-namespace:WpfControls;assembly=WpfControls" 10 Title="RealProbabiTableCompare" > 11 <Grid > 12 13 14 <Grid.Resources> 15 16 <!--背景色改变必须先设置cellStyle 因为cellStyle会覆盖rowStyle样式--> 17 <Style TargetType="DataGridRow"> 18 <Setter Property="Background" Value="#F2F2F2" /> 19 <Setter Property="Height" Value="25"/> 20 <Setter Property="Foreground" Value="Black" /> 21 <Style.Triggers> 22 <!--隔行换色--> 23 <Trigger Property="AlternationIndex" Value="0" > 24 <Setter Property="Background" Value="#e7e7e7" /> 25 </Trigger> 26 <Trigger Property="AlternationIndex" Value="1" > 27 <Setter Property="Background" Value="#f2f2f2" /> 28 </Trigger> 29 30 <Trigger Property="IsMouseOver" Value="True"> 31 <Setter Property="Background" Value="LightGray"/> 32 <!--<Setter Property="Foreground" Value="White"/>--> 33 </Trigger> 34 35 <Trigger Property="IsSelected" Value="True"> 36 <Setter Property="Foreground" Value="Black"/> 37 </Trigger> 38 </Style.Triggers> 39 </Style> 40 <!--原来的样式--> 41 <Style TargetType="TextBlock"> 42 <Setter Property="HorizontalAlignment" Value="Center"/> 43 <Setter Property="VerticalAlignment" Value="Center"/> 44 <Setter Property="FontFamily" Value="Arial" /> 45 <Setter Property="FontSize" Value="13" /> 46 <Setter Property="Foreground" Value="#FFFAF0" /> 47 </Style> 48 49 <Style x:Key="br_bgGray" TargetType="Border"> 50 <Setter Property="Background" Value="#dbdbdb"/> 51 </Style> 52 53 <Style TargetType="Grid" x:Key="MyDataGrid"> 54 <Setter Property="Background" > 55 <Setter.Value> 56 <!--垂直渐变--> 57 <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1"> 58 <GradientStop Color="#555555" Offset="0.0"/> 59 <GradientStop Color="#111111" Offset="0.5"/> 60 <GradientStop Color="#555555" Offset="1.0"/> 61 </LinearGradientBrush> 62 </Setter.Value> 63 </Setter> 64 <Style.Triggers> 65 <Trigger Property="IsMouseOver" Value="True"> 66 <Setter Property="Background" > 67 <Setter.Value> 68 <!--垂直渐变--> 69 <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1"> 70 <GradientStop Color="#FF111111" Offset="0.0"/> 71 <GradientStop Color="#FF333333" Offset="1.0"/> 72 </LinearGradientBrush> 73 </Setter.Value> 74 </Setter> 75 </Trigger> 76 </Style.Triggers> 77 </Style> 78 79 </Grid.Resources> 80 81 <Grid.RowDefinitions> 82 <RowDefinition Height="0" /> 83 <RowDefinition Height="35" /> 84 <RowDefinition Height="80"/> 85 <RowDefinition Height="*"/> 86 <RowDefinition Height="70"/> 87 </Grid.RowDefinitions> 88 89 <!--第一行--> 90 91 <Label Content="概率预报对比" FontSize="20" Foreground="White" Grid.Row="1" Background="Gray"></Label> 92 93 94 <!--第三行放表格头--> 95 <Grid Margin="10,10,10,0" Grid.Row="2" Panel.ZIndex="2" Style="{StaticResource MyDataGrid}" > 96 <Grid.RowDefinitions> 97 <RowDefinition Height="4*" /> 98 <RowDefinition Height="66*" /> 99 </Grid.RowDefinitions> 100 <Grid.ColumnDefinitions> 101 <ColumnDefinition Width="4*"/> 102 <ColumnDefinition Width="8*"/> 103 <ColumnDefinition Width="16*"/> 104 <ColumnDefinition Width="13*"/> 105 <ColumnDefinition Width="18*"/> 106 <ColumnDefinition Width="16*"/> 107 <ColumnDefinition Width="20"/> 108 </Grid.ColumnDefinitions> 109 <Border BorderThickness="1" BorderBrush="Gray" CornerRadius="0" Grid.RowSpan="2"> 110 <TextBlock HorizontalAlignment="Center" >ID</TextBlock> 111 </Border> 112 <Border Grid.Column="1" BorderThickness="1" BorderBrush="Gray" CornerRadius="0" Grid.RowSpan="2"> 113 <TextBlock HorizontalAlignment="Center" >Date</TextBlock> 114 </Border> 115 116 117 118 <Border Grid.Column="2" Grid.ColumnSpan="1" BorderThickness="1" BorderBrush="Gray" CornerRadius="0" Grid.RowSpan="2"> 119 <Grid > 120 <Grid.RowDefinitions> 121 <RowDefinition /> 122 <RowDefinition /> 123 </Grid.RowDefinitions> 124 <Grid.ColumnDefinitions> 125 <ColumnDefinition /> 126 <ColumnDefinition /> 127 <ColumnDefinition /> 128 <ColumnDefinition /> 129 </Grid.ColumnDefinitions> 130 <Border Grid.Row="0" Grid.ColumnSpan="4" BorderThickness="0" BorderBrush="Gray" CornerRadius="0"> 131 <TextBlock Grid.Row="0" Grid.ColumnSpan="2">太阳耀斑概率</TextBlock> 132 </Border> 133 <Border Grid.Row="1" Grid.Column="0" BorderThickness="0,1,1,0" BorderBrush="Gray" CornerRadius="0"> 134 <TextBlock Grid.Row="2" Grid.Column="0">obs</TextBlock> 135 </Border> 136 <Border Grid.Row="1" Grid.Column="1" BorderThickness="0,1,0,0" BorderBrush="Gray" CornerRadius="0"> 137 <TextBlock Grid.Row="2" Grid.Column="1">1d(%)</TextBlock> 138 </Border> 139 <Border Grid.Row="1" Grid.Column="2" BorderThickness="1,1,0,0" BorderBrush="Gray" CornerRadius="0"> 140 <TextBlock Grid.Row="2" Grid.Column="1">2d(%)</TextBlock> 141 </Border> 142 <Border Grid.Row="1" Grid.Column="3" BorderThickness="1,1,0,0" BorderBrush="Gray" CornerRadius="0"> 143 <TextBlock Grid.Row="2" Grid.Column="1">3d(%)</TextBlock> 144 </Border> 145 </Grid> 146 </Border> 147 148 149 <Border Grid.Column="3" Grid.ColumnSpan="1" BorderThickness="1" BorderBrush="Gray" CornerRadius="0" Grid.RowSpan="2"> 150 <Grid > 151 <Grid.RowDefinitions> 152 <RowDefinition /> 153 <RowDefinition /> 154 </Grid.RowDefinitions> 155 <Grid.ColumnDefinitions> 156 <ColumnDefinition Width="4*" /> 157 <ColumnDefinition Width="3*"/> 158 <ColumnDefinition Width="3*"/> 159 <ColumnDefinition Width="3*"/> 160 </Grid.ColumnDefinitions> 161 <Border Grid.Row="0" Grid.ColumnSpan="4" BorderThickness="0" BorderBrush="Gray" CornerRadius="0"> 162 <TextBlock Grid.Row="0" Grid.ColumnSpan="2">质子事件概率</TextBlock> 163 </Border> 164 <Border Grid.Row="1" Grid.Column="0" BorderThickness="0,1,1,0" BorderBrush="Gray" CornerRadius="0"> 165 <TextBlock Grid.Row="2" Grid.Column="0">obs</TextBlock> 166 </Border> 167 <Border Grid.Row="1" Grid.Column="1" BorderThickness="0,1,0,0" BorderBrush="Gray" CornerRadius="0"> 168 <TextBlock Grid.Row="2" Grid.Column="1">1d(%)</TextBlock> 169 </Border> 170 <Border Grid.Row="1" Grid.Column="2" BorderThickness="1,1,0,0" BorderBrush="Gray" CornerRadius="0"> 171 <TextBlock Grid.Row="2" Grid.Column="1">2d(%)</TextBlock> 172 </Border> 173 <Border Grid.Row="1" Grid.Column="3" BorderThickness="1,1,0,0" BorderBrush="Gray" CornerRadius="0"> 174 <TextBlock Grid.Row="2" Grid.Column="1">3d(%)</TextBlock> 175 </Border> 176 </Grid> 177 </Border> 178 179 <Border Grid.Column="4" Grid.ColumnSpan="1" BorderThickness="1" BorderBrush="Gray" CornerRadius="0" Grid.RowSpan="2"> 180 <Grid > 181 <Grid.RowDefinitions> 182 <RowDefinition /> 183 <RowDefinition /> 184 </Grid.RowDefinitions> 185 <Grid.ColumnDefinitions> 186 <ColumnDefinition Width="9*" /> 187 <ColumnDefinition Width="3*" /> 188 <ColumnDefinition Width="3*"/> 189 <ColumnDefinition Width="3*"/> 190 </Grid.ColumnDefinitions> 191 <Border Grid.Row="0" Grid.ColumnSpan="4" BorderThickness="0" BorderBrush="Gray" CornerRadius="0"> 192 <TextBlock Grid.Row="0" Grid.ColumnSpan="2">地磁暴概率</TextBlock> 193 </Border> 194 <Border Grid.Row="1" Grid.Column="0" BorderThickness="0,1,1,0" BorderBrush="Gray" CornerRadius="0"> 195 <TextBlock Grid.Row="2" Grid.Column="0">obs</TextBlock> 196 </Border> 197 <Border Grid.Row="1" Grid.Column="1" BorderThickness="0,1,0,0" BorderBrush="Gray" CornerRadius="0"> 198 <TextBlock Grid.Row="2" Grid.Column="1">1d(%)</TextBlock> 199 </Border> 200 <Border Grid.Row="1" Grid.Column="2" BorderThickness="1,1,0,0" BorderBrush="Gray" CornerRadius="0"> 201 <TextBlock Grid.Row="2" Grid.Column="1">2d(%)</TextBlock> 202 </Border> 203 <Border Grid.Row="1" Grid.Column="3" BorderThickness="1,1,0,0" BorderBrush="Gray" CornerRadius="0"> 204 <TextBlock Grid.Row="2" Grid.Column="1">3d(%)</TextBlock> 205 </Border> 206 </Grid> 207 </Border> 208 209 210 <Border Grid.Column="5" Grid.ColumnSpan="1" BorderThickness="1" BorderBrush="Gray" CornerRadius="0" Grid.RowSpan="2"> 211 <Grid > 212 <Grid.RowDefinitions> 213 <RowDefinition /> 214 <RowDefinition /> 215 </Grid.RowDefinitions> 216 <Grid.ColumnDefinitions> 217 <ColumnDefinition /> 218 <ColumnDefinition /> 219 <ColumnDefinition /> 220 <ColumnDefinition /> 221 </Grid.ColumnDefinitions> 222 <Border Grid.Row="0" Grid.ColumnSpan="4" BorderThickness="0" BorderBrush="Gray" CornerRadius="0"> 223 <TextBlock Grid.Row="0" Grid.ColumnSpan="2">高能电子暴概率</TextBlock> 224 </Border> 225 <Border Grid.Row="1" Grid.Column="0" BorderThickness="0,1,1,0" BorderBrush="Gray" CornerRadius="0"> 226 <TextBlock Grid.Row="2" Grid.Column="0">obs</TextBlock> 227 </Border> 228 <Border Grid.Row="1" Grid.Column="1" BorderThickness="0,1,0,0" BorderBrush="Gray" CornerRadius="0"> 229 <TextBlock Grid.Row="2" Grid.Column="1">1d(%)</TextBlock> 230 </Border> 231 <Border Grid.Row="1" Grid.Column="2" BorderThickness="1,1,0,0" BorderBrush="Gray" CornerRadius="0"> 232 <TextBlock Grid.Row="2" Grid.Column="1">2d(%)</TextBlock> 233 </Border> 234 <Border Grid.Row="1" Grid.Column="3" BorderThickness="1,1,0,0" BorderBrush="Gray" CornerRadius="0"> 235 <TextBlock Grid.Row="2" Grid.Column="1">3d(%)</TextBlock> 236 </Border> 237 </Grid> 238 </Border> 239 240 </Grid> 241 242 <!--第四行放数据--> 243 244 <DataGrid Grid.Row="3" Background="LightGray" Name="dg1" Margin="10,-27,10,0" Grid.Column="0" Grid.RowSpan="1" ItemsSource="{Binding}" AutoGenerateColumns="False" CanUserAddRows="False" HorizontalScrollBarVisibility="Disabled" AlternationCount="2" AreRowDetailsFrozen="True" RowHeaderWidth="10" HeadersVisibility="Column" IsReadOnly="True" HorizontalGridLinesBrush="LightGray" VerticalGridLinesBrush="LightGray" Foreground="#FF9C9D9D" BorderBrush="Black" VerticalScrollBarVisibility="Visible"> 245 246 <DataGrid.CellStyle> 247 <Style TargetType="DataGridCell"> 248 <Setter Property="Height" Value="30" /> 249 <Setter Property="Foreground" Value="Black" /> 250 <!--datagrid 垂直居中、水平左对齐 --> 251 <Setter Property="Template"> 252 <Setter.Value> 253 <ControlTemplate TargetType="{x:Type DataGridCell}"> 254 <Grid Background="{TemplateBinding Background}"> 255 <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/> 256 </Grid> 257 </ControlTemplate> 258 </Setter.Value> 259 </Setter> 260 <!--datagrid 设置选中行的背景色 --> 261 <Style.Triggers > 262 <Trigger Property="IsSelected" Value="True"> 263 <Setter Property="Background" Value="#00B0C4DE"></Setter> 264 </Trigger> 265 </Style.Triggers> 266 </Style> 267 </DataGrid.CellStyle> 268 <DataGrid.Columns> 269 <!--ID列--> 270 <DataGridTemplateColumn Header="ID" Width="4*"> 271 <DataGridTemplateColumn.CellTemplate> 272 <DataTemplate > 273 <Border Height="30" Width="200"> 274 <TextBlock Foreground="Gray" FontSize="16" Height="30" FontFamily="Microsoft YaHei" HorizontalAlignment="Left" Margin="9,5,0,0" Background="Transparent" Text="{Binding ID}"/> 275 </Border> 276 </DataTemplate> 277 </DataGridTemplateColumn.CellTemplate> 278 </DataGridTemplateColumn> 279 <!--时间列--> 280 <DataGridTextColumn Header="Date" FontSize="16" Binding="{Binding dayTime}" Width="8*" Foreground="Gray" FontFamily="Microsoft YaHei"> 281 </DataGridTextColumn> 282 283 <!--<DataGridTextColumn Header="CNum" FontSize="12" Binding="{Binding value}" Width="4*" Foreground="Gray" FontFamily="Microsoft YaHei"> 284 285 </DataGridTextColumn>--> 286 <DataGridTemplateColumn Header="obs" Width="4*" > 287 <DataGridTemplateColumn.CellTemplate> 288 <DataTemplate > 289 <Border Height="30" Width="200" Style="{StaticResource ResourceKey=br_bgGray}"> 290 <TextBlock Foreground="Gray" FontSize="16" Height="30" FontFamily="Microsoft YaHei" HorizontalAlignment="Left" Margin="9,5,0,0" Background="Transparent" Text="{Binding value}"/> 291 </Border> 292 </DataTemplate> 293 </DataGridTemplateColumn.CellTemplate> 294 </DataGridTemplateColumn> 295 <DataGridTextColumn Header="1d" FontSize="16" Binding="{Binding pre1}" Width="4*" Foreground="Gray" FontFamily="Microsoft YaHei"> 296 </DataGridTextColumn> 297 <DataGridTextColumn Header="2d" FontSize="16" Binding="{Binding pre2}" Width="4*" Foreground="Gray" FontFamily="Microsoft YaHei"> 298 </DataGridTextColumn> 299 <DataGridTextColumn Header="3d" FontSize="16" Binding="{Binding pre3}" Width="4*" Foreground="Gray" FontFamily="Microsoft YaHei"> 300 </DataGridTextColumn> 301 302 <!--kp列要更改的位置就是这里--> 303 <!--<DataGridTextColumn Header="CNum" FontSize="12" Binding="{Binding valueu}" Width="4*" Foreground="Gray" FontFamily="Microsoft YaHei"> 304 </DataGridTextColumn>--> 305 306 <DataGridTemplateColumn Header="obs" Width="4*" > 307 <DataGridTemplateColumn.CellTemplate> 308 <DataTemplate > 309 <Border Height="30" Width="200" Style="{StaticResource ResourceKey=br_bgGray}"> 310 <TextBlock Foreground="Gray" FontSize="16" Height="30" FontFamily="Microsoft YaHei" HorizontalAlignment="Left" Margin="9,5,0,0" Background="Transparent" Text="{Binding valueu}"/> 311 </Border> 312 </DataTemplate> 313 </DataGridTemplateColumn.CellTemplate> 314 </DataGridTemplateColumn> 315 <DataGridTextColumn Header="1d" FontSize="16" Binding="{Binding pre1u}" Width="3*" Foreground="Gray" FontFamily="Microsoft YaHei"> 316 </DataGridTextColumn> 317 <DataGridTextColumn Header="2d" FontSize="16" Binding="{Binding pre2u}" Width="3*" Foreground="Gray" FontFamily="Microsoft YaHei"> 318 </DataGridTextColumn> 319 <DataGridTextColumn Header="3d" FontSize="16" Binding="{Binding pre3u}" Width="3*" Foreground="Gray" FontFamily="Microsoft YaHei"> 320 </DataGridTextColumn> 321 322 323 <!--<DataGridTextColumn Header="CNum" FontSize="12" Binding="{Binding valuem}" Width="7*" Foreground="Gray" FontFamily="Microsoft YaHei"> 324 </DataGridTextColumn>--> 325 <DataGridTemplateColumn Header="obs" Width="9*" > 326 <DataGridTemplateColumn.CellTemplate> 327 <DataTemplate > 328 <Border Height="30" Width="200" Style="{StaticResource ResourceKey=br_bgGray}"> 329 <TextBlock Foreground="Gray" FontSize="16" Height="30" FontFamily="Microsoft YaHei" HorizontalAlignment="Left" Margin="9,5,0,0" Background="Transparent" Text="{Binding valuem}"/> 330 </Border> 331 </DataTemplate> 332 </DataGridTemplateColumn.CellTemplate> 333 </DataGridTemplateColumn> 334 335 <DataGridTextColumn Header="1d" FontSize="16" Binding="{Binding pre1m}" Width="3*" Foreground="Gray" FontFamily="Microsoft YaHei"> 336 </DataGridTextColumn> 337 <DataGridTextColumn Header="2d" FontSize="16" Binding="{Binding pre2m}" Width="3*" Foreground="Gray" FontFamily="Microsoft YaHei"> 338 </DataGridTextColumn> 339 <DataGridTextColumn Header="3d" FontSize="16" Binding="{Binding pre3m}" Width="3*" Foreground="Gray" FontFamily="Microsoft YaHei"> 340 </DataGridTextColumn> 341 342 343 <!--<DataGridTextColumn Header="CNum" FontSize="12" Binding="{Binding valuec}" Width="4*" Foreground="Gray" FontFamily="Microsoft YaHei"> 344 </DataGridTextColumn>--> 345 <DataGridTemplateColumn Header="obs" Width="4*" > 346 <DataGridTemplateColumn.CellTemplate> 347 <DataTemplate > 348 <Border Height="30" Width="200" Style="{StaticResource ResourceKey=br_bgGray}"> 349 <TextBlock Foreground="Gray" FontSize="16" Height="30" FontFamily="Microsoft YaHei" HorizontalAlignment="Left" Margin="9,5,0,0" Background="Transparent" Text="{Binding valuec}"/> 350 </Border> 351 </DataTemplate> 352 </DataGridTemplateColumn.CellTemplate> 353 </DataGridTemplateColumn> 354 355 <DataGridTextColumn Header="1d" FontSize="16" Binding="{Binding pre1c}" Width="4*" Foreground="Gray" FontFamily="Microsoft YaHei"> 356 </DataGridTextColumn> 357 <DataGridTextColumn Header="2d" FontSize="16" Binding="{Binding pre2c}" Width="4*" Foreground="Gray" FontFamily="Microsoft YaHei"> 358 </DataGridTextColumn> 359 <DataGridTextColumn Header="3d" FontSize="16" Binding="{Binding pre3c}" Width="4*" Foreground="Gray" FontFamily="Microsoft YaHei"> 360 </DataGridTextColumn> 361 362 363 </DataGrid.Columns> 364 </DataGrid> 365 366 367 368 <!--第五行放数据处理后的值--> 369 <StackPanel Grid.Row="4" Orientation="Horizontal" HorizontalAlignment="Center"> 370 <Label Content="From:" HorizontalContentAlignment="Right" Style="{StaticResource QueryLabel}"></Label> 371 <DatePicker Width="90" Height="25" x:Name="Dp_Start" BorderThickness="1" HorizontalAlignment="Center" VerticalContentAlignment="Center" /> 372 373 <Label Content="To:" HorizontalContentAlignment="Right" Style="{StaticResource QueryLabel}"></Label> 374 <DatePicker Width="90" Height="25" x:Name="Dp_End" BorderThickness="1" HorizontalAlignment="Center" VerticalContentAlignment="Center" /> 375 <Button Name="Bt_CheckCurr" Style="{StaticResource Button_Image}" Width="38" Height="38" HorizontalAlignment="Left" ToolTip="查询" > 376 <Image Source="/NetReform;component/Images/Search.png" Width="35" Height="35" /> 377 </Button> 378 </StackPanel> 379 </Grid> 380 </bp:BasePage>
作者:Ants_double
出处:https://www.cnblogs.com/ants_double/
本文版权归作者和博客园所有,欢迎转载。转载请在留言板处留言给我,且在文章标明原文链接,谢谢!
如果您觉得本篇博文对您有所收获,觉得我还算用心,请点击右下角的 [大拇指],谢谢!