Oto XAML:Oprawa z ElementName nie działa
<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 zwraca błąd: System.Windows.Data Błąd: 2: Nie można odnaleźć regulujące FrameworkElement lub FrameworkContentElement do elementu docelowego.
Czego mi brakuje?
Aktualizacja:
Oto odpowiedź: 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.
Aktualizacja 2: Dobry artykuł o zastrzeżeniami DataGrid: http://blogs.msdn.com/b/vinsibal/archive/2009/04/07/5-random-gotchas-with-the-wpf-datagrid.aspx
Nie sądzę 'ElementName' działa * * w ciągu tego samego elementu . – ChrisF
Oto odpowiedź: http://blogs.msdn.com/b/jaimer/archive/2008/11/22/forwarding-the-datagrid-s-datacontext-to-its-columns.aspx > Co dzieje się tutaj? Kolekcja kolumn to po prostu własność > w Datagrid; kolekcja ta nie znajduje się w logicznym (lub wizualnym) drzewie, dlatego DataContext nie jest dziedziczona, co prowadzi do tego, że nie ma nic do wiązania. –