01概述
WPF 自带了一个表格控件datagrid,这个控件类似winfrom中的datagridview,在数据显示的时候也经常会用到,这节主要讲解如何从后台代码获取到单元格控件并改变其相关属性:背景色、前景色、字体等。
02效果演示
03代码
后台cs:
using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
namespace Caliburn.Micro.Hello
{
/// <summary>
/// ShellView.xaml 的交互逻辑
/// </summary>
public partial class ShellView : UserControl
{
public ShellView()
{
InitializeComponent();
}
private void DG_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
{
int colindex = -1;
int rowindex = -1;
//方法1
//DataGridCellInfo info = new DataGridCellInfo(dg.Items[0], dg.Columns[2]);
//方法2
//foreach (DataGridCellInfo info in this.dgSourceData.SelectedCells)
//{
// string str = ((TextBlock)info.Column.GetCellContent(info.Item)).Text;
// Console.WriteLine(str);
//}
//方案1
var info = this.dgSourceData.SelectedCells.FirstOrDefault();
var str = ((TextBlock)info.Column.GetC