开发者

WPF binding screentip with relative source

开发者 https://www.devze.com 2023-01-15 19:49 出处:网络
I was expecting the title of the screen tip to show \"X\" as well, but it is empty: <Fluent:Button x:Name=\"rbNewProject\"

I was expecting the title of the screen tip to show "X" as well, but it is empty:

<Fluent:Button x:Name="rbNewProject"
        Header="X">

    <Fluent:Button.ToolTip>
        <Fluent:ScreenTip Title="{Binding Header, RelativeSource={RelativeSource FindAncestor, Ance开发者_运维知识库storType=Fluent:Button}}">
        </Fluent:ScreenTip>
    </Fluent:Button.ToolTip>

</Fluent:Button>

I suspect my binding expression is wrong, but I can't figure it out...


Unfortunately, FindAncestor doesn't work on ToolTips because they aren't part of their target element's VisualTree. What you can do is set the DataContext of the ToolTip to be its PlacementTarget (i.e. Fluent:Button in your example) so that other Binding statements for the tooltip works with the PlacementTarget as the binding source.

In your case, try this code:

<Fluent:Button x:Name="rbNewProject"
        Header="X">

    <Fluent:Button.ToolTip>
        <Fluent:ScreenTip DataContext="{Binding PlacementTarget, RelativeSource={RelativeSource Self}}" Title="{Binding Header}">
        </Fluent:ScreenTip>
    </Fluent:Button.ToolTip>

</Fluent:Button>

More information on this "technique" can be found here: http://karlshifflett.wordpress.com/2007/12/29/wpf-sample-series-data-binding-in-tooltip/

0

精彩评论

暂无评论...
验证码 换一张
取 消