我似乎无法在 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/