C# DataGrid后台绑定到锯齿阵列
我有一个数组C# DataGrid后台绑定到锯齿阵列,c#,wpf,arrays,datagrid,C#,Wpf,Arrays,Datagrid,我有一个数组byte[][]colormapping,我想操纵一个数据网格,使它绑定到数组(但我相信不是传统意义上的)。我不想在网格中显示数组的内容,而是希望根据数组的值更改特定单元格背景(我希望单元格本身为空) 例如,如果colormapping[0][0]==27,那么我希望[0][0]处的datagrid单元格具有特定的颜色背景 此外,随着数组值的改变,我希望datagrid的背景也相应地改变 我知道这是一个一般性的问题,但我该怎么做呢 更新: 这是我目前拥有的。它用字节本身填充数据网格(
byte[][]colormapping
,我想操纵一个数据网格,使它绑定到数组(但我相信不是传统意义上的)。我不想在网格中显示数组的内容,而是希望根据数组的值更改特定单元格背景(我希望单元格本身为空)
例如,如果colormapping[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();
}
}
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
//mt.BinaryMapping是一个字节[][]
dataGrid1.ItemsSource=BindingHelper.GetBindable2ViewFromIList(mt.BinaryMapping);
}
私有void dataGrid_AutoGeneratingColumn(对象发送方,DataGridAutoGeneratingColumnEventArgs e)
{
DataGridTextColumn=e.列作为DataGridTextColumn;
绑定=列。绑定为绑定;
binding.Path=新属性路径(binding.Path.Path+“.Value”);
}
}
公共类参考
{
私有只读函数getter;
私有只读操作设置器;
公共引用(Func getter、Action setter)
{
this.getter=getter;
this.setter=setter;
}
公共T值{get{return getter();}set{setter(Value);}}
}
公共类BindingHelper
{
公共静态数据视图GetBindable2ViewfromIList(IListList2D)
{
DataTable=新的DataTable();
对于(int i=0;i<((IList)list2d[0])。计数;i++)
{
Add(i.ToString(),typeof(Ref));
}
for(int i=0;i(list2d[a]作为IList)[b],z=>{(list2d[a]作为IList)[b]=z;});
数据视图[i][j]=refT;
}
}
返回数据视图;
}
}
公共类ColorConverter:IValueConverter
{
公共对象转换(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性)
{
字节s=(字节)值;
如果(s==255)
{
返回笔刷。深粉色;
}
如果(s==237),则为else
{
返回笔刷。蓝色;
}
其他的
{
返回刷。白色;
}
}
公共对象转换回(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性)
{
抛出新的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>
您可以使用CellFormatting事件设置背景色,而无需将阵列绑定到网格 在我的测试中,我使用了颜色数组而不是字节数组
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];
}
}
Color[,]colormapping=新颜色[8,9];
公共表格1()
{
初始化组件();
对于(int i=0;i
您是从什么地方开始的,还是要求我们为您做这项工作?您需要在后台使用setter设置DataGrid.CellStyle,其值为,value={Binding Path=,Converter={静态资源SomeHexToBrushConverter@devhedgehog我已经更新了我的问题,到目前为止,我不相信WPF中有CellFormatting。对不起,错过了“WPF”标记。(原始问题没有任何代码)