Is this a bug I can't see how it is a feature.

Scenario 1 (Works - the checkbox is hidden):
<CheckBox Visibility="{Binding Path=ReturnsFalse, Converter={StaticResource BooleanToVisibility}}" />

Scenario 2 (Doesn't work - the checkbox remains visible)
<CheckBox DataContext="{Binding Path=DataContextObject}" Visibility="{Binding Path=ReturnsFalse, Converter={StaticResource BooleanToVisibility}}" />

Why does my visibility call stop working when i add a DataContext I can supply more information if needed but it is pretty cut and dry. I have a GridView that is dynamically populated. The last column is a list of actions (edit, delete, view) and I want to be able to dynamically show/hide one/all of these actions if the user has access to edit/delete/view that particular row.

What are my options to solve my problem

Re: Windows Presentation Foundation (WPF) GridView bind Visibility and DataContext


In both scenarios, the DataContext property determines the source of your binding since you are not setting the source explicitly (via the Source, ElementName, or RelativeSource binding properties).

In the first scenario, the DataContext is inherited from the visual parent and the binding points to the "ReturnsFalse" property on this inherited context.

In the second scenario, you are explicitly setting the DataContext to some property called DataContextObject. We don't know what the type of this property is, but I'm guessing it doesn't have a "ReturnsFalse" property. This is why your binding stops working. If you run under a debugger, you should see debug output that explains why the binding fails. It will likely say that the ReturnsFalse property can't be found.

To recommend a solution for your scenario, we will need more details.

Re: Windows Presentation Foundation (WPF) GridView bind Visibility and DataContext

Mike Brown

Essentially, you probably don't even need the explicit DataContext binding on the Checkbox, because you're not using it within the Checkbox itself. And Checkboxes don't (usually) have children other than those supporting their display. But the good Dr. is correct, we can only guess why it's not working without seeing the full *ahem* context.

Re: Windows Presentation Foundation (WPF) GridView bind Visibility and DataContext

Yiling Lai

Hello, I don't think you need to set Path property in your DataContext. Try this:

<CheckBox DataContext="{StaticResource DataContextObject}" Visibility="{Binding Path=ReturnsFalse, Converter={StaticResource BooleanToVisibility}}" />


<CheckBox DataContext="{Binding Source={StaticResource DataContextObject}}" ...../>