我有这个,我知道绑定工作,因为我用它来填充数据网格,但是当我尝试填充行标题时,它似乎不起作用。
<DataGrid Margin="150,50,150,50" x:Name="GridBinding" ItemsSource="{Binding Path=Elements[Month]}" Background="Transparent" RowBackground="Transparent" AutoGenerateColumns="False" AreRowDetailsFrozen="True" SelectionUnit="Cell" GridLinesVisibility="None" IsReadOnly="True">
<DataGrid.Columns>
<DataGridTextColumn Header="Oct." Binding="{Binding Path=Element[October].Value}" Width="*" Foreground="Black"/>
<DataGridTextColumn Header="Nov." Binding="{Binding Path=Element[November].Value}" Width="*" Foreground="Black"/>
<DataGridTextColumn Header="Dec." Binding="{Binding Path=Element[December].Value}" Width="*" Foreground="Black"/>
<DataGridTextColumn Header="Jan." Binding="{Binding Path=Element[January].Value}" Width="*" Foreground="Black"/>
<DataGridTextColumn Header="Feb." Binding="{Binding Path=Element[February].Value}" Width="*" Foreground="Black"/>
<DataGridTextColumn Header="Mar." Binding="{Binding Path=Element[March].Value}" Width="*" Foreground="Black"/>
<DataGridTextColumn Header="Apr." Binding="{Binding Path=Element[April].Value}" Width="*" Foreground="Black"/>
<DataGridTextColumn Header="May" Binding="{Binding Path=Element[May].Value}" Width="*" Foreground="Black"/>
<DataGridTextColumn Header="June" Binding="{Binding Path=Element[June].Value}" Width="*" Foreground="Black"/>
<DataGridTextColumn Header="July" Binding="{Binding Path=Element[July].Value}" Width="*" Foreground="Black"/>
<DataGridTextColumn Header="Aug." Binding="{Binding Path=Element[August].Value}" Width="*" Foreground="Black"/>
<DataGridTextColumn Header="Sep." Binding="{Binding Path=Element[September].Value}" Width="*" Foreground="Black"/>
</DataGrid.Columns>
<DataGrid.RowHeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Element[Depth].Value, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGridRowHeader}}" Foreground="#9493CF" FontSize="16" />
</DataTemplate>
</DataGrid.RowHeaderTemplate>
答案 0 :(得分:1)
您不需要Ancestor
,因为RowHeader
也具有与普通列相同的DataContext
。所以你可以直接在那里获得Element
财产。
但在你的情况下,它不起作用......你可能会因为某些事情而错。无论如何,下面的代码将帮助您解决。
<DataGrid.RowHeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=DataContext.Element[Depth].Value, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGridRow}}" Foreground="#9493CF" FontSize="16" />
</DataTemplate>
</DataGrid.RowHeaderTemplate>