Jonathan
Jonathan

Reputation: 817

Trouble finding correct relative binding syntax for DataGrid.ItemsSource from TabItemHeaderTemplate

I have a TabControl in my Control with in each tab a DataGrid. I am styling the tab header using a template. I would like to bind to the DataGrid.ItemsSource on the tab to be able to apply a filter to add an image to the TabItem indicating a status.

Below is my VisualTree with an arrow indicating the binding:

enter image description here

And this is my template with the relative binding, which unfortunately is not working.

<DataTemplate x:Key="TabItemHeaderWithIconTemplate">
    <StackPanel Orientation="Horizontal">
        <Image>
            <Image.Style>
                <Style TargetType="{x:Type Image}">
                    <Style.Triggers>
                        <DataTrigger 
                            Value="True" 
                            Binding="{Binding Path=ItemsSource, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Converter={StaticResource StatusHasFailureConverter}, Mode=OneWay}">
                            <Setter Property="Source" Value="/REDACTED;component/Resources/red-x-icon.png"/>
                        </DataTrigger>
                        <DataTrigger 
                            Value="False" 
                            Binding="{Binding Path=ItemsSource, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Converter={StaticResource StatusHasFailureConverter}, Mode=OneWay}">
                            <Setter Property="Source" Value="/REDACTED;component/Resources/green-checkmark-icon.png"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Image.Style>
        </Image>
        <TextBlock Text="{Binding}" />
    </StackPanel>
</DataTemplate>

If I remove the relative binding and directly bind to a CollecionViewSource defined in my resources the StatusHasFailureConverter is triggered and the icon is visible and matches the output of my converter, but with my relative binding no icon appears at all, the converter seems not to be triggered, but also no binding issues are raised.

Any pointers on how to define the binding to point to the topmost DataGrid ItemsSource in the TabControl (indicated with the arrow)?

Upvotes: 0

Views: 89

Answers (1)

Keith Stein
Keith Stein

Reputation: 6754

It seems because of the way TabControl works, the contents of tabs are not children of their items (only the headers are children of the individual items). This means a relative binding would not be possible as far as I can tell. I would instead recommend binding the DataGrid and TabItem header to he same common source, which it seems you've already figured out how to do.

Upvotes: 1

Related Questions