我有一个数组byte[][] colourMapping
,我想操纵一个DataGrid,使它绑定到数组(但我认为不是传统意义上的)。我不想在网格中显示数组的内容,而是想根据数组的值更改特定的单元格背景(我希望单元格本身为空白)。
例如,如果colourMapping [0] [0] == 27,那么我希望[0] [0]处的datagrid单元格具有一定的颜色背景。
此外,随着数组值的变化,我希望datagrid的背景会相应改变。
我知道这是一个普遍的问题,但我该如何做呢?
更新:
这是我现在拥有的。它用字节本身填充数据网格(这是我想要避免的),并且似乎没有设置背景:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
//mt.BinaryMapping is a byte[][]
dataGrid1.ItemsSource = BindingHelper.GetBindable2DViewFromIList<byte>(mt.BinaryMapping);
}
private void dataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
DataGridTextColumn column = e.Column as DataGridTextColumn;
Binding binding = column.Binding as Binding;
binding.Path = new PropertyPath(binding.Path.Path + ".Value");
}
}
public class Ref<T>
{
private readonly Func<T> getter;
private readonly Action<T> setter;
public Ref(Func<T> getter, Action<T> setter)
{
this.getter = getter;
this.setter = setter;
}
public T Value { get { return getter(); } set { setter(value); } }
}
public class BindingHelper
{
public static DataView GetBindable2DViewFromIList<T>(IList list2d)
{
DataTable dataTable = new DataTable();
for (int i = 0; i < ((IList)list2d[0]).Count; i++)
{
dataTable.Columns.Add(i.ToString(), typeof(Ref<T>));
}
for (int i = 0; i < list2d.Count; i++)
{
DataRow dataRow = dataTable.NewRow();
dataTable.Rows.Add(dataRow);
}
DataView dataView = new DataView(dataTable);
for (int i = 0; i < list2d.Count; i++)
{
for (int j = 0; j < ((IList)list2d[i]).Count; j++)
{
int a = i;
int b = j;
Ref<T> refT = new Ref<T>(() => (list2d[a] as IList<T>)[b], z => { (list2d[a] as IList<T>)[b] = z; });
dataView[i][j] = refT;
}
}
return dataView;
}
}
public class ColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
byte s = (byte)value;
if (s == 255)
{
return Brushes.DeepPink;
}
else if (s == 237)
{
return Brushes.Blue;
}
else
{
return Brushes.White;
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
}
}
这是我的DataGrid,它显示单元格中的字节(不良行为,但可管理),似乎无法正确设置背景颜色:
<DataGrid AutoGenerateColumns="True" AutoGeneratingColumn="dataGrid_AutoGeneratingColumn" HorizontalAlignment="Left" Margin="6,6,0,0" Name="dataGrid1"
VerticalAlignment="Top" HeadersVisibility="None" RowHeaderWidth="0" ColumnHeaderHeight="0" IsReadOnly="True" HorizontalScrollBarVisibility="Disabled"
VerticalScrollBarVisibility="Disabled" SelectionMode="Extended" SelectionUnit="Cell">
<DataGrid.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="Background" Value="{Binding Path=Content, Converter={StaticResource ColorConverter}}" />
</Style>
</DataGrid.CellStyle>
</DataGrid>
答案 0 :(得分:0)
您可以使用CellFormatting事件设置背景颜色,而无需将数组绑定到网格。
对于我的测试,我使用了Color而不是byte的数组。
Color[,] colourMapping = new Color[8,9];
public Form1()
{
InitializeComponent();
for (int i = 0; i < colourMapping.GetLength(0); i++)
{
for (int j = 0; j < colourMapping.GetLength(1); j++)
{
colourMapping[i,j] = Color.Beige;
}
}
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.RowIndex < colourMapping.GetLength(0) && e.ColumnIndex < colourMapping.GetLength(1))
{
e.CellStyle.BackColor = colourMapping[e.RowIndex, e.ColumnIndex];
}
}