WPF-Datagrid常用
列标题居中和单元格内容居中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <!--DataGrid 列标题居中方法--> <Style x:Key= "ColumnHeaderStyle" TargetType= "DataGridColumnHeader" > <Setter Property= "HorizontalContentAlignment" Value= "Center" /> <Setter Property= "Background" Value= "Gray" /> <Setter Property= "Foreground" Value= "White" /> </Style> <!--DataGrid单元格内容居中--> <Style x:Key= "DataGridCellStyle" TargetType= "DataGridCell" > <Setter Property= "HorizontalAlignment" Value= "Center" /> <Setter Property= "VerticalAlignment" Value= "Center" /> </Style> <colorConverter:StringToColorConverter x:Key= "DataGridColorConverter" /> |
常用样式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | <!-- 列头标题栏样式 --> <Style TargetType= "DataGridColumnHeader" > <Setter Property= "HorizontalContentAlignment" Value= "Center" /> <Setter Property= "Background" Value= "Gray" /> <Setter Property= "Foreground" Value= "White" /> <Setter Property= "BorderThickness" Value= "1" /> <Setter Property= "BorderBrush" Value= "Gray" /> <Setter Property= "Height" Value= "30" /> <Setter Property= "FontSize" Value= "12" /> </Style> <!-- 单元格样式 --> <Style TargetType= "DataGridCell" > <Setter Property= "FocusVisualStyle" Value= "{x:Null}" /> <Setter Property= "BorderThickness" Value= "0" /> <Setter Property= "BorderBrush" Value= "Gray" /> <Setter Property= "FontSize" Value= "12" /> <Setter Property= "Template" > <Setter.Value> <ControlTemplate TargetType= "{x:Type DataGridCell}" > <Grid Background= "{TemplateBinding Background}" > <ContentPresenter HorizontalAlignment= "Center" VerticalAlignment= "Center" /> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style> <Style TargetType= "TextBlock" x:Key= "centerAlignmentStyle" > <Setter Property= "Foreground" Value= "Black" /> <Setter Property= "FontSize" Value= "12" /> <Setter Property= "HorizontalAlignment" Value= "Center" /> <Setter Property= "VerticalAlignment" Value= "Center" /> <Setter Property= "TextWrapping" Value= "Wrap" /> </Style> <!--背景色改变必须先设置cellStyle 因为cellStyle会覆盖rowStyle样式,换行换色--> <Style TargetType= "DataGridRow" > <Setter Property= "Height" Value= "30" /> <Style.Triggers> <Trigger Property= "AlternationIndex" Value= "0" > <!--单元格背景色--> <Setter Property= "Background" Value= "White" /> <!--ffe14d--> </Trigger> <Trigger Property= "AlternationIndex" Value= "1" > <!--<Setter Property= "Background" Value= "#f2f2f2" />--> <Setter Property= "Background" Value= "Blue" /> <!--f1ef9f--> </Trigger> <Trigger Property= "IsMouseOver" Value= "True" > <Setter Property= "Background" Value= "#f1ef9f" /> </Trigger> <Trigger Property= "IsSelected" Value= "True" > <Setter Property= "Background" Value= "#05c4ff" /> </Trigger> <Trigger Property= "IsFocused" Value= "True" > <Setter Property= "Background" Value= "#05c4ff" /> </Trigger> </Style.Triggers> </Style> <!--网格线颜色--> <Style TargetType= "DataGrid" > <!--该属性指示是否允许用户调整列宽度--> <Setter Property= "CanUserResizeColumns" Value= "false" /> <!--<Setter Property= "Background" Value= "#2d323c" />--> <Setter Property= "Background" Value= "White" /> <Setter Property= "BorderBrush" Value= "Gray" /> <Setter Property= "SelectionUnit" Value= "FullRow" /> <Setter Property= "VerticalScrollBarVisibility" Value= "Auto" /> <Setter Property= "HorizontalScrollBarVisibility" Value= "Auto" /> <Setter Property= "RowHeaderWidth" Value= "0" /> <Setter Property= "HorizontalGridLinesBrush" > <Setter.Value> <SolidColorBrush Color= "#d6c79b" /> </Setter.Value> </Setter> <Setter Property= "VerticalGridLinesBrush" > <Setter.Value> <SolidColorBrush Color= "#d6c79b" /> </Setter.Value> </Setter> </Style> |
每一行添加序号
1,在XAML页面中的DataGrid中添加一列
1 | <DataGridTextColumn Header= "序号" Width= "*" Binding= "{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type DataGridRow}}, Path=Header}" CellStyle= "{StaticResource DataGridCellStyle}" /> |
2,在DataGrid上添加一个LoadingRow事件,该事件的处理方法如下
1 2 3 4 | private void DataGrid_LoadingRow( object sender, DataGridRowEventArgs e) { e.Row.Header = e.Row.GetIndex() + 1; } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构