WPF Datagrid display conditional Image

Sep 16, 2009 at 10:54 AM
Edited Sep 16, 2009 at 10:54 AM
Hi, I have a datagrid of which the ItemsSource is set to a collection of custom Person objects. The Person class has a certain property (IsPreferent) which returns a boolean. What I try to achieve is to have a column in the datagrid which displays a certain icon only if the IsPreferent property returns True. I have set up a DataGridTemplateColumn with an Image element in the DataTemplate, but do not know how to achieve my desired behaviour. I have tried using a Style for the Image element and using a DataTrigger to determine the value of IsPreferent, and then setting the Visibility property accordingly. But I have trouble setting up the Binding and can't make it work. Anyone have any ideas? Thanks. P.
Sep 18, 2009 at 9:54 PM

Hi PeterMS,

It seems like what you stated should work.  Setting up a databinding to the bool on the Visibility property should accomplish this.  Could you please post a code snippet so we have a better idea on what the issue could be?


Saied Khanahmadi

Sep 19, 2009 at 7:20 AM
Edited Sep 19, 2009 at 7:21 AM
Thanks for the reply.

I managed to solve it. I was not binding to the Visibility property but tried to use a DataTrigger. After reading more on WPF Databinding I found the answer. I now bind to the Visibility property of the image and use a converter to convert the bool from my IsPreferent property to a Visibility value. Works like a charm.