WPF中工具提示样式的DataTemplate的DataContext [英] DataContext of DataTemplate in ToolTip Style in WPF

查看:146
本文介绍了WPF中工具提示样式的DataTemplate的DataContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法在ToolTipStyle样式中为网格找到正确的DataContext。

I can't seem to find the right DataContext for the Grid in the ToolTipStyle Style. Just a blank ToolTip appears.

<Window.Resources>
    <DataTemplate x:Key="ListTemplate">
        <StackPanel>
            <Grid>
                <TextBlock Text="{Binding Path=Name}">
                    <TextBlock.ToolTip>
                        <ToolTip Style="{StaticResource ToolTipStyle}" />
                    </TextBlock.ToolTip>
                </TextBlock>
            </Grid>
        </StackPanel>
    </DataTemplate>

    <Style TargetType="ToolTip" x:Key="ToolTipStyle">
        <Setter Property="ContentTemplate">
            <Setter.Value>
                <DataTemplate>
                    <Grid>
                        <TextBlock Text="{Binding Path=Description}" />
                    </Grid>
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Windows.Resources>

<ListBox ItemTemplate="{StaticResource ListTemplate}" />


推荐答案

工具提示/弹出窗口似乎不存在于可视化树之外。我看到很多人绑定到PlacementTarget属性以获取返回。

Tooltips /Popups seem to exist outside of the visual tree. I see a lot of people bind to the PlacementTarget property to get back.


DataContext = {Binding Path = PlacementTarget.DataContext,
RelativeSource = {RelativeSource Self}}

DataContext="{Binding Path=PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}"



<DataTemplate x:Key="ListTemplate">
    <StackPanel>
        <Grid>
            <TextBlock>
                <TextBlock.ToolTip>
                    <ToolTip DataContext="{Binding Path=PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}" Text="{Binding Path=Name}" Style="{StaticResource ToolTipStyle}" />
                </TextBlock.ToolTip>
            </TextBlock>
        </Grid>
    </StackPanel>
</DataTemplate>

这篇关于WPF中工具提示样式的DataTemplate的DataContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆