7

Here is the XAML:

<DataGrid Grid.Column="0"  AutoGenerateColumns="False" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ItemsSource="{Binding Path=Columns}"
              x:Name="ColumnsGrid" RowHeaderWidth="0">
        <DataGrid.Columns>
            <DataGridTextColumn Width="*" Binding="{Binding Path=Header}" 
                                Header="{Binding ElementName=ColumnsGrid, Path=DataContext.Count, StringFormat=Columns ({0}), diag:PresentationTraceSources.TraceLevel=High}"/>
        </DataGrid.Columns>  
    </DataGrid>

Binding returns error: System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element.

What do I miss?

Update:

Here is the answer: http://blogs.msdn.com/b/jaimer/archive/2008/11/22/forwarding-the-datagrid-s-datacontext-to-its-columns.aspx

What is happening here? The Columns collection is just a property in the Datagrid; this collection is not in the logical (or visual) tree, therefore the DataContext is not being inherited, which leads to there being nothing to bind to.

Update 2: Good article about DataGrid's caveats: http://blogs.msdn.com/b/vinsibal/archive/2009/04/07/5-random-gotchas-with-the-wpf-datagrid.aspx

Pavel Voronin
  • 13,503
  • 7
  • 71
  • 137
  • 1
    I don't think `ElementName` works *within* the same element. – ChrisF Nov 01 '11 at 09:11
  • Here is the answer: http://blogs.msdn.com/b/jaimer/archive/2008/11/22/forwarding-the-datagrid-s-datacontext-to-its-columns.aspx > What is happening here? The Columns collection is just a property > in the Datagrid; this collection is not in the logical (or visual) > tree, therefore the DataContext is not being inherited, which leads to > there being nothing to bind to. – Pavel Voronin Nov 01 '11 at 09:39
  • yet another funny bug that proves the only thing WPF is good for is writing tutorials, this technology is driving me crazy. – lot Jan 27 '21 at 18:13

2 Answers2

1

Binding on DataGridColumn for Header abd Visibility properties needs special treatment...

See this...

Bind datagrid column visibility MVVM

Community
  • 1
  • 1
WPF-it
  • 19,625
  • 8
  • 55
  • 71
  • Yep this worked for me too. `ProxyElement` approach is a very good hack. :-) –  Nov 01 '11 at 10:52
0

If you are interested in the Count property of the object that is referred to in the DataContext you try and use regular databinding:

<DataGrid Grid.Column="0"  AutoGenerateColumns="False" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ItemsSource="{Binding Path=Columns}"
          x:Name="ColumnsGrid" RowHeaderWidth="0">
    <DataGrid.Columns>
        <DataGridTextColumn Width="*" Binding="{Binding Path=Header}" 
                            Header="{Binding Path=Count, StringFormat=Columns ({0}), diag:PresentationTraceSources.TraceLevel=High}"/>
    </DataGrid.Columns>  
</DataGrid>

EDIT

Apparently the columns of a datagrid are not part of the Visual Tree So using ElementName and RelativeSource will not work. Perhaps you should add the property to the object the column is bound to.

Emond
  • 50,210
  • 11
  • 84
  • 115