1
votes

I seem to be having problems trying to bind the header column of a DataGridTextColumn. This code works fine when I have no TabControl/TabItem wrapping it but when I put it in the TabControl it can no longer find the DayHeader. I'd imagine it's a problem with the FindAncestor/AncestoryType but I'm not sure what to do to fix it or if that really is the issue. Any help would be appreciated.

<!--Not Working
<TabControl Margin="0,25,0,0" Background="{x:Null}">
    <TabItem >
        <Grid >
            <DataGrid></DataGrid>
        </Grid>
    </TabItem>
    <TabItem Header="Test Header">-->

<!--Working-->
<Grid>
    <DataGrid ItemsSource="{Binding RunningViewSource.View}" Margin="0,27,0,-5" SelectionMode="Single">
        <DataGrid.ItemsPanel>
            <ItemsPanelTemplate>
                <VirtualizingStackPanel VirtualizingStackPanel.VirtualizationMode="Recycling" />
            </ItemsPanelTemplate>
         </DataGrid.ItemsPanel>
         <DataGrid.Columns>
             <DataGridTextColumn Header="Contingencies" Binding="{Binding Contingencies}"
                                            IsReadOnly="True" Width="400" />
             <DataGridTextColumn Binding="{Binding Days[4]}" CellStyle="{StaticResource NumberCell}">
                 <DataGridTextColumn.Header>
                     <TextBlock Text="{Binding DataContext.DayHeader, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}">
                     </TextBlock>
                  </DataGridTextColumn.Header>
             </DataGridTextColumn>
         </DataGrid.Columns>
     </DataGrid>
</Grid>

<!--Not Working
    </TabItem>
</TabControl>-->

This is the message I get in the output window if I add the non working parts.

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.DataGrid', AncestorLevel='1''. BindingExpression:Path=DataContext.DayHeader; DataItem=null; target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')

Edit: I can only reproduce this error when I have a TabControl with more than one TabItem. I've updated my code. Sorry for any confusion but adding the other TabItem is what makes the DayHeader unreachable.

Edit2: I know a lot of people use Snoop to help them with binding issues. Well when I inspect the column headers with Snoop, the text magically appears when I have them highlighted. I have no idea why this works so hopefully someone with more knowledge about Snoop will be able to help. Here is code behind for accessing the DayHeader and maybe that will help.

//Code Behind
 private string dayHeader;

    public string DayHeader
    {
        get { return dayHeader; }
        set
        {
            dayHeader = value;
            NotifyOfPropertyChange(() => DayHeader);
        }
    }

Thanks for any help.

1

1 Answers

3
votes

DataGridTextColumn.Header is not in the visual tree,so it's not inherting DataContext.You can use Freezable class as it's shown in this article.