简体   繁体   中英

Binding not working with row header on DataGrid

I have this and I know the binding work because I use it to populate the datagrid, but when I try to populate the row header it doesn't seem to work.

        <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>

You doesn't require the Ancestor since the RowHeader also has the same DataContext where the normal column has. So you could directly get the Element Property there.

在此处输入图片说明

But in your case its not working means.. you might be gone with some thing wrong. Anyway the below code will help you to resolve.

 <DataGrid.RowHeaderTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Path=DataContext.Element[Depth].Value, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGridRow}}"  Foreground="#9493CF" FontSize="16" />
        </DataTemplate>
    </DataGrid.RowHeaderTemplate>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM