Is there a simple way to display an icon in a DataGrid field according to a Boolean value?
Granted, a converter is required to convert a Boolean to Visibility but can you show an example of the required xaml.
For example, here is what I do for a simple GridView:
<GridViewColumn Width="30" Header="MyHeader" > <GridViewColumn.CellTemplate> <DataTemplate> <Image Source="/MyProject;component/Resources/Document.png" Width="16" Height="16" Margin="1" Visibility="{Binding IsVisible, Converter={StaticResource booleanToVisibleCollapsedConverter}}" /> </DataTemplate> </GridViewColumn.CellTemplate> </GridViewColumn>
Hello Jean-Marc Da Pozzo,
In order to display an icon in a XamDataGrid field according to a bool value, I would recommend that you place your "bool value" on your data item as a property and define an Unbound TemplateField to display it. You can read about configuring Unbound and Template fields at the following online documentation articles:
Unbound Field: https://es.infragistics.com/help/wpf/xamdatapresenter-add-unbound-fields-to-a-datapresenter-control.
Template Field: https://es.infragistics.com/help/wpf/xamdatagrid-configuring-template-field.
Once you do this, you can use a DisplayTemplate on your TemplateField to display your image. I would then recommend that you bind the Visibility property of your Image to the bool property on your underlying data item. You can do this by utilizing a RelativeSource AncestorType binding to the ancestral CellValuePresenter element, and then use a binding path of DataContext.DataItem.<PropertyName>. Breaking this binding path down, the DataContext of the CellValuePresenter is the DataRecord that contains its corresponding cell. This DataRecord has a DataItem property that returns the data item that represents that record, which, in this case, would have your bool property that determines whether or not the image should be shown. From here, you can use a BooleanToVisibilityConverter to convert the bool property to a valid Visibility value.
I have attached a sample project to demonstrate the above. I hope this helps.
Please let me know if you have any other questions or concerns on this matter.
Sincerely,AndrewAssociate Developer
Works great, sample provided was easy to understand and worked perfectly!
Thanks