zoukankan      html  css  js  c++  java
  • WPF DataGrid ListView 等等 改变 选中行 颜色;以及 不变的原因

    WPF中改变选中行的颜色是很简单的,就是用触发器:比如:以DataGrid为例: DataGrid.RowStyle Style TargetType= DataGridRow SetterProperty= Background Value= White / Style .Triggers TriggerProperty= IsMouseOver Value= True SetterProperty= Background Value= LightGray / /Trigger TriggerPrope

      

      WPF中改变选中行的颜色是很简单的,就是用触发器:比如:以DataGrid为例:

    <DataGrid.RowStyle >
                                    <Style TargetType="DataGridRow">
                                        <Setter Property="Background" Value="White"/>
                                        <Style.Triggers>
                                            <Trigger Property="IsMouseOver" Value="True">
                                                <Setter Property="Background" Value="LightGray"/>
                                            </Trigger>
                                            <Trigger Property="IsSelected" Value="True">
                                                <Setter Property="Background" Value="LightGray"/>
                                                <Setter Property="Foreground" Value="Red"/>
                                            </Trigger>
                                        </Style.Triggers>
                                    </Style>                                
        </DataGrid.RowStyle>

     

      但是,我告诉你,如果你没有设置cellStyle,你将会发现上面的代码“貌似”不工作,没用。这一点对 ListView 等等相似控件都是一样的。

      事实上,上面代码已经起作用了。IsSelected确实被触发了。问题在哪呢?

      答案正确,就在CellStyle上。因为RowStyle 的背景改变了,但CellStyle没变,你是看不出来的。

      也就是RowStyle很冤,因为CellStyle是在其上的。所以会被覆盖。原因就是“黑人”“白人”穿着“黑衣服”看起来都是黑的。

      改变的方法如下:

      加入CellStyle:

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

  • 相关阅读:
    HDU 5486 Difference of Clustering 图论
    HDU 5481 Desiderium 动态规划
    hdu 5480 Conturbatio 线段树 单点更新,区间查询最小值
    HDU 5478 Can you find it 随机化 数学
    HDU 5477 A Sweet Journey 水题
    HDU 5476 Explore Track of Point 数学平几
    HDU 5475 An easy problem 线段树
    ZOJ 3829 Known Notation 贪心
    ZOJ 3827 Information Entropy 水题
    zoj 3823 Excavator Contest 构造
  • 原文地址:https://www.cnblogs.com/bdbw2012/p/3914262.html
Copyright © 2011-2022 走看看