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

Gishu
  • 134,492
  • 47
  • 225
  • 308
Matt Randle
  • 1,885
  • 2
  • 17
  • 22

4 Answers4

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.

Matt Randle
  • 1,885
  • 2
  • 17
  • 22
  • 8
    Here is the example of using StaticResource proxy to forward DataContext: http://tomlev2.wordpress.com/2011/03/21/wpf-how-to-bind-to-data-when-the-datacontext-is-not-inherited/ – surfen Dec 13 '11 at 21:00
  • i am using MVVM and it worked finde with the provided link: http://www.thomaslevesque.com/2011/03/21/wpf-how-to-bind-to-data-when-the-datacontext-is-not-inherited/ – Pascalsz Jul 09 '13 at 10:53
1

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

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Guge
  • 4,569
  • 4
  • 35
  • 47
  • 2
    Yes. forwarding datacontext is the way to go. Similar but IMHO simpler approach: http://tomlev2.wordpress.com/2011/03/21/wpf-how-to-bind-to-data-when-the-datacontext-is-not-inherited/ – surfen Dec 13 '11 at 20:59
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.

Kent Boogaart
  • 175,602
  • 35
  • 392
  • 393
0

If you can bind from code you can use

BindingOperations.SetBinding(DatagridColumInstance,
                             DatagridColum.VisibilityProperty,
                             myNewBindDef);
sth
  • 222,467
  • 53
  • 283
  • 367
Joseph
  • 11
  • 4