xaml - WPF中ToolTip样式的DataTemplate的DataContext

标签 xaml styles tooltip datatemplate datacontext

我似乎无法在 ToolTipStyle 样式中找到适合网格的 DataContext。仅出现一个空白的工具提示。

<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}" />

最佳答案

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

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>

关于xaml - WPF中ToolTip样式的DataTemplate的DataContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14161504/

相关文章:

wpf - 如何在 WPF 中创建不可选择的 TreeViewItem

c# - 从仅系统托盘的应用程序创建工具提示

c# - 无法绑定(bind)到文本 block - Windows Phone - MVVM

c# - WPF 将组合框绑定(bind)到 LINQ 填充的可观察集合

c# - 在 WPF 的代码隐藏中为 ListBox 创建 ItemTemplate

xml - 如何协调 Visual Studio 注释期望与具有 Doxygen 注释的代码?

javascript - Highcharts 同步图表与其他图表结合

c# - 以 double 存储十进制数?使用 updatesourcetrigger 作为 PropertyChanged 的​​属性

c# - 更新 silverlight 中的属性绑定(bind)

javascript - 未捕获的类型错误 : Cannot read property 'style' of null - JS error