繁体   English   中英

绑定不适用于DataGrid上的行标题

[英]Binding not working with row header on DataGrid

我有这个,我知道绑定工作,因为我用它来填充数据网格,但是当我尝试填充行标题时,它似乎不起作用。

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

您不需要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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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