I'm having a problem understanding the basics of databinding in WPF. I have a generic DataGrid (with AutoGenerateColumns set) that is bound to a DataTable with column names that vary on every load. When the dataTable contains columns that are of type boolean, I want to render a column that contains custom images representing true and false.
To accomplish this, I have a StaticResource declared on the page for the celltemplate, and I have c# code that traps the AutoGenerateColumn event and uses this template:
<DataTemplate x:Key="CheckmarkColumnTemplate">
<Image x:Name="CheckmarkImage" Source="..\..\images\check.png" Height="16" Width="16" />
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Value}" Value="False">
<Setter TargetName="CheckmarkImage" Property="Source" Value="..\..\images\nocheck.png" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
C# code:
private void dgData_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
if (e.PropertyType == typeof(bool))
{
DataGridTemplateColumn col = new DataGridTemplateColumn();
Binding binding = new Binding(e.PropertyName);
col.CellTemplate = (this.Resources["CheckmarkColumnTemplate"] as DataTemplate);
col.Header = e.PropertyName;
e.Column = col;
}
}
This mostly works, except I've got the DataTrigger Binding property messed up. It never detects when the value of the column is "false", so it never shows the nocheck.png image. I don't know how to write the Binding property so that's referencing the databound value of the column (remember, the column name is different every time, so I can't hard-code a column name in the Path part of the binding).
Can anyone tell me what the Binding property should look like so that it just grabs the value of the column?