I want to bind the Array to a 4-column DataGrid and update the data. Is this possible?
If it can't be achieved, how do I need to modify it?
For automatic updates, it seems that we can bind to some collection of objects with editable values is to define a class for the item. But i don't know how to do it.
public partial class MainWindow : Window
{
int X=18;
public MainWindow()
{
InitializeComponent();
int[] arr = new int[] { 61, 61, 61, 61, 61, 20, 30, 40, 50, 100, 200, 300, 400, 500, 0, 0, 0, 0, 0, 18, 23, 65, 41, 22, 91, 64, 33, 18, 44, 63, 91, 26, 32, 61, 83, 91, 26, 32, 91, 91, 91, 91 };
DataGrid dataGrid = new DataGrid();
dataGrid.AutoGenerateColumns = false;
dataGrid.ItemsSource = arr.ToList();
for (int i = 1; i < 5; i++)
{
DataGridTemplateColumn column = new DataGridTemplateColumn();
column.Header = "Value"+i;
DataTemplate cellTemplate = new DataTemplate();
FrameworkElementFactory textBlock = new FrameworkElementFactory(typeof(TextBlock));
textBlock.SetBinding(TextBlock.TextProperty, new Binding("."));
cellTemplate.VisualTree = textBlock;
column.CellTemplate = cellTemplate;
DataTemplate cellEditingTemplate = new DataTemplate();
FrameworkElementFactory textBox = new FrameworkElementFactory(typeof(TextBox));
textBox.SetBinding(TextBox.TextProperty, new Binding("."));
cellEditingTemplate.VisualTree = textBox;
column.CellEditingTemplate = cellEditingTemplate;
dataGrid.Columns.Add(column);
}
canvas.Children.Add(dataGrid);
Canvas.SetLeft(dataGrid, X);
}
}
The result:
I want a result like this:
61, 61, 61, 61, 61,
20, 30, 40, 50, 100,
200, 300, 400, 500, 0,
0, 0, 0, 0, 18,

Value1tovalue4and have an array of this class instead of an array of int.GetItems(arr);. I don't know how to use Array and class collection together