WPF Show data from multiple DataContexts in ToolTip of ItemsControl

后端 未结 5 1967
清酒与你
清酒与你 2021-01-18 03:54

I am trying to display a tooltip for an item generated by an ItemsControl that needs to pull data from conceptually unrelated sources. For example, say I have a

5条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-18 04:52

    Almost correct Yacoder, and guessed way wrong there Dabblernl ;)

    Your way of thinking is correct and it is possible to reference the DataContext of your ItemsControl

    You are missing the DataContext property in path:

    {Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}, Path=DataContext.GlobalText}
    

    Second attempt ;)

    http://blogs.msdn.com/tom_mathews/archive/2006/11/06/binding-a-tooltip-in-xaml.aspx

    Here is an article with the same problem. They can reference the DataContext of their Parent control by the PlacementTarget property:

    
    

    If you would place the DataContext on a deeper level, you avoid changing your Item DataContext

    A second suggestion (Neil and Adam Smith) was that we could use PlacementTarget in the binding. This is nice, as I am actually inheriting the DataContext already from the page that hosts the DataControl, and this would allow the ToolTip to gain access back to the origial control. As Adam noted, though, you have to be aware of the parent/child structure off your markup:

提交回复
热议问题