鼠标移动,改变datagrid颜色

Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs)
   
        If e.Item.ItemType = ListItemType.Item Or _
            e.Item.ItemType = ListItemType.AlternatingItem Then
   
         '---------------------------------------------------
         ' Add the OnMouseOver and OnMouseOut method to the Row of DataGrid
         '---------------------------------------------------
         e.Item.Attributes.Add("onmouseover", "this.style.backgroundColor= 'Silver'")
            e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor= 'white'")
   
        End If
   
    End Sub
   
    Sub DataGrid2_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs)
   
        If e.Item.ItemType = ListItemType.Item Or _
            e.Item.ItemType = ListItemType.AlternatingItem Then
   
         '---------------------------------------------------
         ' Add the OnMouseOver and OnMouseOut method a Cell (Column) of DataGrid
         '---------------------------------------------------
         e.Item.Cells(1).Attributes.Add("onmouseover", "this.style.backgroundColor= '#DDEEFF'")
            e.Item.Cells(1).Attributes.Add("onmouseout", "this.style.backgroundColor= 'white'")
   
         '---------------------------------------------------
         ' Change the Mouse Cursor of a particular Cell (Column) of DataGrid
         ' (Or you may do it for a whole Row of DataGrid :)
         '---------------------------------------------------
         e.Item.Cells(3).Style("cursor") = "hand"
   
         '---------------------------------------------------
         ' Add the OnClick Alert MessageBox to a particular Cell (Column) of DataGrid
         '---------------------------------------------------
         e.Item.Cells(3).Attributes.Add("onclick", "alert('You click at ID: " & e.Item.Cells(0).Text & "!');")
   
        End If
    End Sub

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WPF的DataGrid可以通过绑定来实现单元格字体颜色改变。在XAML中,可以通过在DataGrid的CellStyle中设置Trigger来实现,如下所示: ``` xml <DataGrid> <DataGrid.CellStyle> <Style TargetType="{x:Type DataGridCell}"> <Style.Triggers> <DataTrigger Binding="{Binding Path=YourBindingProperty}" Value="YourValue"> <Setter Property="Foreground" Value="Red"/> <!-- 设置字体颜色为红色 --> </DataTrigger> <Style.Triggers> </Style> </DataGrid.CellStyle> </DataGrid> ``` 其中,YourBindingProperty是绑定到DataGrid中的数据源的属性,当属性值等于YourValue时,就会触发Trigger中的设置,将字体颜色设置为红色。这里只是简单的例子,实际情况中可能需要根据不同属性设置不同的颜色。 另外,也可以通过继承DataGrid来自定义单元格样式。在自定义的DataGrid中,可以重写GetCellContainer方法来获取单元格,然后通过设置单元格的Foreground属性来改变字体颜色,如下所示: ``` csharp public class MyDataGrid : DataGrid { protected override System.Windows.DependencyObject GetContainerForCellOverride() { return new MyDataGridCell(); } } public class MyDataGridCell : DataGridCell { protected override void OnBindingChanged(System.Windows.DependencyPropertyChangedEventArgs e) { base.OnBindingChanged(e); Brush foreground; // 根据不同的属性设置不同的颜色 if(/* Your condition */) { foreground = Brushes.Red; } else if(/* Your condition */) { foreground = Brushes.Green; } else { foreground = Brushes.Black; } // 设置字体颜色 this.Foreground = foreground; } } ``` 通过自定义DataGridCell,并重写OnBindingChanged方法实现根据不同属性设置不同颜色,然后通过设置字体颜色改变单元格字体颜色。 以上是两种实现DataGrid单元格字体颜色改变的方法,可以根据实际需要选择使用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值