如何在双击时更改单元格的背景颜色?

时间:2013-12-11 05:53:53

标签: .net wpf xaml c#-4.0 radgridview

代码:

<Grid>
    <telerik:RadGridView x:Name="HierarchialGridView" AutoGenerateColumns="False">
        <telerik:RadGridView.Columns>
            <telerik:GridViewDataColumn DataMemberBinding="{Binding Id}" Header="Id" />
            <telerik:GridViewDataColumn DataMemberBinding="{Binding Name}" Header="Name" />
        </telerik:RadGridView.Columns>
    </telerik:RadGridView>
</Grid>

双击单元格后,现在显示默认的白色。 如何在XAML代码中双击单元格时更改单元格的背景颜色?

1 个答案:

答案 0 :(得分:1)

如果您想在xaml中执行此操作,我认为您至少需要重新模板化网格或单元格。

但如果你对一点代码隐藏感到满意:

  • 添加和处理RadGrid的MouseDoubleClick事件

然后:

private void RadGridView_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
     FrameworkElement originalSender = e.OriginalSource as FrameworkElement;
     if (originalSender != null)
     {
          var cell = originalSender.ParentOfType<GridViewCell>();
          if (cell != null)
          {
              cell.Background = new SolidColorBrush(Colors.Red);
          }
     }
}

修改

订阅PreparedCellForEdit活动并:

private void RadGridView_PreparedCellForEdit(object sender, GridViewPreparingCellForEditEventArgs e)
{
    var editingControl = e.EditingElement as Control;
    if(editingControl !=null)
       editingControl.Background = new SolidColorBrush(Colors.Red);

}