How to hide a control if the underlying DataContext is null?

Harry picture Harry · Mar 16, 2011 · Viewed 17.3k times · Source

I have an object in my view model that has a bunch of properties, some of them will occasionally be null. I don't want to just show some controls if these particular controls are null. How would I go about hiding the control if the bind is null? I was thinking of some sort of converter, but don't know how I'd go about doing it exactly. Any ideas?

edit: sorry, I should mention that this will also be in Silverlight, so I'm not sure if Style triggers would work...?

Answer

bugfixr picture bugfixr · Mar 14, 2012

This approach is easier:

<CheckBox Visibility="{Binding Path=checkedField, TargetNullValue=Collapsed }">

When the bound property checkedField is null, the Visibility will be set to Collapsed.