Search code examples
wpfbindingmvvmtooltipitemscontrol

Tooltip with ItemsControl doesn´t update on DataContext change


I got stuck with something that looked like simple mvvm binding.

I have a grid with a tooltip. DataContext of the Grid is changing and should update values in textBlock and the ItemsControl placed inside the tooltip. Problem is that the list inside the tooltip doesn't update. Just for test I added the same ItemsControl below the textblock. This list updates without problems. Here is simplified code that contains only what's necessary (I think so).

<Grid DataContext="{Binding SelectedRouting}">
    <StackPanel>
        <TextBlock Text="{Binding ActionDescription}" />
        <ItemsControl ItemsSource="{Binding RoutingActionList}" ItemTemplate="{StaticResource SingleActionTemplate}"/>
    </StackPanel>

    <Grid.ToolTip>
        <ToolTip Style="{StaticResource ActionToolTipStyle}">
            <ItemsControl ItemsSource="{Binding RoutingActionList}" ItemTemplate="{StaticResource SingleActionTemplate}"/>
        </ToolTip>
    </Grid.ToolTip>
</Grid>

The question is: Why first ItemsControl (the one in StackPanel) updates when Main Grid DataContext is changed and the second ItemsControl Inside the ToolTip doesn't. Any ideas or solutions?


Solution

  • Look at the answer for this question, may be this is what you want - WPF Tooltip does not update

    Additionally in case you want to set tooltip within tooltip you can set the dataContext of your tooltip and it should work then -

     <Grid.ToolTip>
            <ToolTip DataContext="{Binding SelectedRouting}" Style="{StaticResource ActionToolTipStyle}">
                <ItemsControl ItemsSource="{Binding RoutingActionList}" ItemTemplate="{StaticResource SingleActionTemplate}"/>
            </ToolTip>
        </Grid.ToolTip>
    

    Since the tooltip does not belong to the visual tree of your Control, hence changes are not get propagated to it. So, by setting the dataContext for your tooltip, you make it notifiable for PropertyChanges in Datacontext.