Binding Visible property of a DataGridColumn in WPF DataGrid
Asked Answered
P

4

3

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

Pyretic answered 21/3, 2009 at 9:57 Comment(0)
P
8

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.

Pyretic answered 21/3, 2009 at 11:35 Comment(2)
Here is the example of using StaticResource proxy to forward DataContext: tomlev2.wordpress.com/2011/03/21/…Wholewheat
i am using MVVM and it worked finde with the provided link: thomaslevesque.com/2011/03/21/…Zaria
K
1

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.

Kaleykaleyard answered 21/3, 2009 at 10:2 Comment(0)
L
1

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

Laggard answered 22/6, 2010 at 11:45 Comment(1)
Yes. forwarding datacontext is the way to go. Similar but IMHO simpler approach: tomlev2.wordpress.com/2011/03/21/…Wholewheat
F
0

If you can bind from code you can use

BindingOperations.SetBinding(DatagridColumInstance,
                             DatagridColum.VisibilityProperty,
                             myNewBindDef);
Fowler answered 16/2, 2010 at 18:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.