I had a problem understanding the basics of data binding in WPF. I have a generic DataGrid (with a set of AutoGenerateColumns) that is bound to a DataTable with column names that differ for each load. When the dataTable contains boolean columns, I want to display a column containing custom images representing true and false.
For this, I have a StaticResource declared on the page for celltemplate, and I have C # code that catches 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 basically works, except that my DataTrigger Binding property is confused. It never detects that the column value is false, so it never displays the image nocheck.png. I donโt know how to write the Binding property to refer to the value of the column database (remember that the column name is different every time, so I canโt hardcode the column name in part of the binding path).
Can someone tell me what the Binding property should look like so that it just captures the column value?
source share