How to set a PlacementTarget for a WPF tooltip without messing up the DataContext?

前端 未结 3 2192
离开以前
离开以前 2021-02-10 18:19

I have a typical MVVM setup of Listbox and vm + DataTemplate and item vm\'s. The data templates have tooltips, which have elements bound to the item vm\'s. All works great.

3条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-10 19:05

    ToolTips are not part of the visual tree as they are popup based. So your placement target biding (which uses Visual Tree search) to get the relative ancestor wont work. Why not use ContentHacking instead? This way one hacks into the visual tree from the logical elements such as ContextMenu, Popups, ToolTip etc...

    1. Declare a StaticResource which is any FrameworkElement (we need support for data context).

      
              
      
      
    2. Supply a content control in the Visual Tree and set this static resource "ProxyElement" as its content.

      
              
                      
                      
      

    What the above steps have done that "ProxyElement" has been connected to the ItemsControl (which serves as a DataContext) and it is available as a SaticResource to be used anywhere.

    1. Now use this StaticResource as a source for any bindings which are failing in your tooltip...

      
               
      

    and

        
                
                        
                         ...
                
        
    

    Let me know if this helps...

提交回复
热议问题