我被一些看起来像简单的 mvvm 绑定(bind)的东西困住了。
我有一个带有工具提示的网格。网格的 DataContext 正在更改,应该更新 textBlock 和放置在工具提示内的 ItemsControl 中的值。问题是工具提示内的列表不会更新。只是为了测试,我在文本 block 下方添加了相同的 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>
问题是:为什么当主网格 DataContext 更改时第一个 ItemsControl(StackPanel 中的那个)会更新,而工具提示内的第二个 ItemsControl 不会更新。有什么想法或解决方案吗?
最佳答案
看看这个问题的答案
,也许这就是你想要的 - 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。
关于wpf - 带 ItemsControl 的工具提示不会随 DataContext 更改而更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8137034/