【问题标题】:How to Bind Tooltip's DataTemplate to its Parent?如何将 Tooltip 的 DataTemplate 绑定到其父级?
【发布时间】:2018-09-13 13:37:23
【问题描述】:

我编写了以下代码以在工具提示窗口中的ListBoxItem 的内容中显示图像(一种预览效果)。但他们根本不工作。

<ListBox>
  <ListBoxItem>
    <Image x:Name="image" Source="image.jpg" Stretch="Uniform">
      <Image.ToolTip>
        <Image Source="{Binding RelativeSource={RelativeSource AncestorType=Image}, Path=Source}" />
      </Image.ToolTip>
    </Image>
  </ListBoxItem>  
<ListBox>  
<ListBox>
  <ListBoxItem>
    <Image x:Name="image" Source="image.jpg" Stretch="Uniform">
      <Image.ToolTip>
        <Image Source="{Binding Source, RelativeSource={RelativeSource TemplatedParent}}" />
      </Image.ToolTip>
    </Image>
  </ListBoxItem>  
<ListBox>  
<ListBox>
  <ListBoxItem>
    <Image x:Name="image" Source="image.jpg" Stretch="Uniform">
      <Image.ToolTip>
        <Image Source="{Binding}" />
      </Image.ToolTip>
    </Image>
  </ListBoxItem>  
<ListBox>  

这些都不起作用。

【问题讨论】:

    标签: wpf xaml data-binding templatebinding


    【解决方案1】:

    这应该可行:

    <ListBox>
        <ListBoxItem>
            <Image x:Name="image" Source="screen.png" Stretch="Uniform">
                <Image.ToolTip>
                    <ToolTip>
                        <Image Source="{Binding RelativeSource={RelativeSource AncestorType=ToolTip}, Path=PlacementTarget.Source}" />
                    </ToolTip>
                </Image.ToolTip>
            </Image>
        </ListBoxItem>
    </ListBox>
    

    Image 不是ToolTip 的视觉祖先,但您可以使用ToolTipPlacementTarget 属性绑定到Image

    【讨论】:

      猜你喜欢
      • 2013-09-20
      • 2010-11-29
      • 2012-01-01
      • 1970-01-01
      • 2011-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多