【问题标题】:Tooltip with ItemsControl doesn´t update on DataContext change带有 ItemsControl 的工具提示不会在 DataContext 更改时更新
【发布时间】:2011-11-15 13:26:42
【问题描述】:

我被一些看起来像简单的 mvvm 绑定的东西卡住了。

我有一个带有工具提示的网格。 Grid 的 DataContext 正在发生变化,应该更新 textBlock 中的值和放置在工具提示内的 ItemsControl。问题是工具提示内的列表没有更新。只是为了测试,我在文本块下方添加了相同的 ItemsControl。此列表更新没有问题。这是只包含必要内容的简化代码(我认为是这样)。

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

问题是:为什么第一个 ItemsControl(StackPanel 中的那个)会在 Main Grid DataContext 更改时更新,而 ToolTip 中的第二个 ItemsControl 却没有。有什么想法或解决方案吗?

【问题讨论】:

    标签: wpf binding mvvm tooltip itemscontrol


    【解决方案1】:

    answer这个问题,可能这就是你想要的-WPF Tooltip does not update

    此外,如果您想在工具提示中设置工具提示,您可以设置工具提示的 dataContext,然后它应该可以工作 -

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

    由于工具提示不属于控件的可视化树,因此更改不会传播到它。因此,通过为您的工具提示设置 dataContext,您可以通知 Datacontext 中的 PropertyChanges。

    【讨论】:

    • 您编写的解决方案在 VS 输出中给出:找不到提供 DataContext 的元素...
    【解决方案2】:

    我最好的猜测是 WPF 将 ToolTips 读取为 WPF 中不同层的一部分,因此当调用 PropertyChange 事件时,它只会在主层上触发

    尝试为您的 ToolTip 对象指定 DataContext 以便它收到 PropertyChange 通知的警报,或者尝试完全删除它

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

    或者

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

    【讨论】:

    • 您的第一个建议在 VS 输出中给出与 RV1987 中相同的答案找不到提供 DataContext 的元素。第二次尝试几乎可以。在下一篇文章中休息
    【解决方案3】:

    解决方案在 RV1987 提供的链接中。 &lt;Grid.ToolTip&gt; 内不能有&lt;ToolTip&gt; 标签,它解决了我问的问题。 无论如何,还有一个小问题。移除标签也移除了为工具提示指定样式的可能性。无论您在工具提示中放置什么,都会有一个默认边框可见。为我的工具提示分配样式,即漂亮的深色背景。现在它消失了 :( :( 有什么想法吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-26
      • 2013-11-01
      • 2011-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-10
      相关资源
      最近更新 更多