views:

1976

answers:

7

Hi,

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

A: 

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.

HTH, Kent

Kent Boogaart
+1  A: 

Hi,

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.

Matt

Matt Randle
A: 

Hi Matt,

Can you post your code for how to set Source property of binding expression to a StaticResource you mentioned above? I'm facing the same problem with DataGridTextColumn, I just want to bind the Visibility property to a property in my view model, but I got an AG_E_BAD_PARSER runtime error when I did this.

Thanks a lot!

Sissy Zhang
A: 

Hi Matt,

I have tried with setting Source property to a StaticResource but the visibility property sets only once when application starts. After that when i changed my property(ColumnVisible) in ViewModel it doesn't changes the visibility of datagrid Column. can anybody Help me.

Thanks a lot

Sukhjeet
A: 

Hi Matt, Can you post your code..Coz wen I changed it into StaticResource it did not work!!

Thanks, Shrads

Shrads
A: 

If you can bind from code you can use

BindingOperations.SetBinding(DatagridColumInstance,
                             DatagridColum.VisibilityProperty,
                             myNewBindDef);
Joseph
A: 

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

Guge