zoukankan      html  css  js  c++  java
  • WPF 设置DataGrid 选中的背景色和前景色

    <DataGrid >
    
                <DataGrid.CellStyle>
                    <Style TargetType="DataGridCell">
                        <Style.Triggers>
    
                            <Trigger Property="IsSelected" Value="True">
                                <Setter Property="Background" Value="Red"/>
                                <Setter Property="Foreground" Value="Yellow"/>
                            </Trigger>
    
                        </Style.Triggers>
    
                    </Style>
    
                </DataGrid.CellStyle>
    
            </DataGrid>
    

      

    代码动态设置:

    Style styleCells = new Style(typeof(System.Windows.Controls.DataGridCell));
                //styleCells.Setters.Add(new Setter
                //{
                //    Property = System.Windows.Documents.TextElement.ForegroundProperty,
                //    Value = new SolidColorBrush(System.Windows.Media.Colors.Black)
                //});
    
                Trigger triger = new  Trigger()
                {
                    Property = DataGridCell.IsSelectedProperty,
                    Value = true
                };
                triger.Setters.Add(new Setter()
                {
                    Property = Control.BackgroundProperty,
                    Value = new SolidColorBrush(System.Windows.Media.Colors.Red)
                });
                triger.Setters.Add(new Setter()
                {
                    Property = Control.ForegroundProperty,
                    Value = new SolidColorBrush(System.Windows.Media.Colors.White)  
                });
    
                styleCells.Triggers.Add(triger);
    
    
                this.dataGrid.CellStyle = styleCells;
    

      

    代码设置datagrid 列头部(DataGridColumnHeader)的颜色:

    Style chdStyle = new Style(typeof(System.Windows.Controls.Primitives.DataGridColumnHeader));
                chdStyle.Setters.Add(new Setter
                {
                    Property = System.Windows.Documents.TextElement.ForegroundProperty,
                    Value = new SolidColorBrush(System.Windows.Media.Colors.Black)
                });
                this.dataGrid.ColumnHeaderStyle = chdStyle;
    

      

    或者修改ui:

         <DataGrid.ColumnHeaderStyle>
                    <Style TargetType="DataGridColumnHeader">
                        <Style.Setters>
                            <Setter Property="Background" Value="Red"></Setter>
                            <Setter Property="Foreground"  Value="White"></Setter>
                        </Style.Setters>
                    </Style>
                  </DataGrid.ColumnHeaderStyle> 
    

      

    fffffffffffffffff
    test red font.
  • 相关阅读:
    [2013腾讯马拉松 3月23日]HDU 4517 小小明系列故事——游戏的烦恼
    金山西山居初赛第三场 HDU 4551~HDU 4553
    Redis安装
    前端常用网址汇总
    Html5移动端页面布局通用模板暨移动端问题总结
    js数组去重,并统计最多项算法
    纯css实现下拉菜单
    js实现求平均数功能
    Html5+css3实现3D转动效果
    移动设备分辨率及响应式断点汇总
  • 原文地址:https://www.cnblogs.com/wgscd/p/14759657.html
Copyright © 2011-2022 走看看