Matt Randle
Matt Randle

Reputation: 1893

Binding Visible property of a DataGridColumn in WPF DataGrid

I cannot bind the Visible property of the WPF datagridtextcolumn to a boolean value.

My binding expression is,

{Binding Path=DataContext.IsThisColumnVisible, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window},Converter={StaticResource vc}}

I have checked that the converter works (bool to the visibility enum) and is in scope.

If I use the same expression for the header of the column, the header displays 'false' as expected.

Visible is a dependency property so should be bindable.

Anyone see what Im doing wrong? Or has anyone else been able to bind to the visible property.

Regards,

Matt

Upvotes: 3

Views: 13486

Answers (4)

Guge
Guge

Reputation: 4599

I was looking for the same thing and found an execellent way to do it in an article about forwarding datacontext to columns.

Upvotes: 1

Matt Randle
Matt Randle

Reputation: 1893

I worked this out.

DataGridCOlumn is not a framework element so the FindAncestor call was failing (DataGridColumn not part of visual tree)

Have to set source property of binding expression to a staticresource and it works fine.

Upvotes: 8

Kent Boogaart
Kent Boogaart

Reputation: 178630

Hard to say from so little of your code. What is in Visual Studio's Output window (under Debug)? That will often give you a clue as to the problem.

Upvotes: 1

Joseph
Joseph

Reputation: 11

If you can bind from code you can use

BindingOperations.SetBinding(DatagridColumInstance,
                             DatagridColum.VisibilityProperty,
                             myNewBindDef);

Upvotes: 0

Related Questions