I have to perform the entire thing in code, and am at loss after spending two says in StackOverflow and dear uncle google.
I have a class TestData:InotifyPropertyChanged
which includes a string
"Name", another string
"Specifics" and an bool array
"checks". This class is used to build a static Observablecollection
"testData" (lowercase).
I'd like to create by code a datagrid
, not let it make automatic columns, and present these columns:
DataGridTextColumn
with "Name", non-editable.DataGridTextColumn
with "Specifics", editable (and presenting a default value "-").DataGridCheckBoxColum
s with the "checks" data.The strucutre is set-up properly, and displays the required headers and the placeholders for the data. There's a button which adds new records to "testData", and additional rows are indeed added to the datagrid, but sadly these are empty.
I need this to be two-way, obviously.
I can't manage to display the values I have set in the declaration of the "testData" for "specifics" and "checks".
In addition, I keep receiving an error saying that "two way binding requires Path or x:path". It's frustrating...
Here's what I tried:
<DataGrid x:Name="dgMainDataGrid" CanUserAddRows="False" AutoGenerateColumns="False" />
and
dgMainDataGrid.ItemsSource = testData;
DataGridTextColumn col1 = new DataGridTextColumn();
col1.Header = "col1Header";
col1.IsReadOnly = true;
col1.Binding = new Binding("Name");
DataGridTextColumn col2 = new DataGridTextColumn();
col2.Header = "col2Header";
col2.IsReadOnly = false;
col2.Binding = new Binding("Specifics");
DataGridCheckBoxColumn col3 = new DataGridCheckBoxColumn();
col3.Header = "col3Header";
col3.IsReadOnly = false;
col3.Binding = new Binding("checks[0]");
DataGridCheckBoxColumn col4 = new DataGridCheckBoxColumn();
col4.Header = "col3Header";
col4.IsReadOnly = false;
col4.Binding = new Binding("checks[1]");
DataGridCheckBoxColumn col5 = new DataGridCheckBoxColumn();
col5.Header = "col3Header";
col5.IsReadOnly = false;
col5.Binding = new Binding("checks[2]");
and class
public class TestData : INotifyPropertyChanged
{
public string Name;
public string Specifics;
public bool[] checks;
}
Thanks, guys.
After @Clemens asked how my class looks, I saw these were not properties. Just sprinkle some {get; set;} over the fields.