这里是XAML:用的ElementName绑定不起作用
<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>
绑定返回错误:System.Windows.Data错误:2:找不到理事FrameworkElement的或FrameworkContentElement上的目标元素。
我错过了什么?
更新:
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.
更新2: 约DataGrid的告诫好文章:http://blogs.msdn.com/b/vinsibal/archive/2009/04/07/5-random-gotchas-with-the-wpf-datagrid.aspx
我不认为'ElementName'工作*同一个元素中。 – ChrisF
下面是答案: http://blogs.msdn.com/b/jaimer/archive/2008/11/22/forwarding-the-datagrid-s-datacontext-to-its-columns.aspx >什么在这里发生? Columns集合只是Datagrid中的一个属性 >;这个集合不在逻辑(或可视化)树中,因此DataContext没有被继承,这导致没有任何东西需要绑定。 –