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