I'm still new to Databinding and have been reading and researching this question for hours now, and I'm hoping someone can at least point me in the right direction.
What I have is a DataTable populated with the objects such as:
public class SimpleObject
{
public string DisplayValue { get; set; }
public bool Match { get; set; }
public string BackGroundColor
{
get { if (Match) return "Green"; else return "White"; }
set { //do nothing }
}
}
I've set up my headers for the columns of the datatable as such:
DataTable MyDataTable = new DataTable()
List headers = new List<string>() {"Header1", "Header2", "Header3", "Header4"}
foreach (string key in headers)
{
MyDataTable.Columns.Add(new DataColumn(key, typeof(SimpleObject)));
}
And populated my DataTable rows by adding rows similar to:
SimpleObject[] rowList = new SimpleObject[4]
DataRow dataRow = MyDataTable.NewRow();
for(int i = 0; i < 4; i++)
{
//Not really how I determine values, but this will do for a basic example
rowList[i].DisplayValue = i.ToString();
rowList[i].Match = i % 2 == 0;
}
dataRow.ItemArray = rowList;
MyDataTable.Rows.Add(dataRow);
SimpleDataGrid.DataContext = MyDataTable;
Now, what I want to do is bind MyDataTable to the DataGrid such that:
- SimpleObject.DisplayValue is shown in the cell's value
- the background color of the cell is determined by SimpleObject.BackGroundColor
If anyone could give me advice as to how to this, I would much appreciate it! So far I've tried doing something similar to this:
<DataGrid Name="SimpleDataGrid"
AutoGenerateColumns="False"
ItemsSource="{Binding}">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Header1" Width="100">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Header1.DisplayValue}"
BackGround="{Binding Path=Header1.BackGroundColor}"
/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
But had no luck. I can't even get the DisplayValue to bind to the Text (even without trying to bind BackGroundColor). Any help or direction would be greatly appreciated!