代码之家  ›  专栏  ›  技术社区  ›  Nostromo

来自背景对象的工具提示

wpf
  •  0
  • Nostromo  · 技术社区  · 9 月前

    我喜欢在背景图像上显示一些位置。为此,我放了一个 ListBox (与a Canvas 作为 ItemsPanel )在A之上 Image 内部a Grid ,类似于:

    <Grid>
        <Image Source="{Binding ImageSource}"
               ToolTipService.ToolTip="{Binding ImageToolTip}" />
    
        <ListBox ItemsSource="{Binding ListSource}"
                 Background="Transparent">
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <Canvas IsItemsHost="True" />
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
            <ListBox.ItemContainerStyle>
                <Style TargetType="{x:Type ListBoxItem}">
                    <Setter Property="Canvas.Left" Value="{Binding Left}" />
                    <Setter Property="Canvas.Top" Value="{Binding Top}" />
                    <Setter Property="Padding" Value="0" />
                    <Setter Property="Margin" Value="0" />
                </Style>
            </ListBox.ItemContainerStyle>
            <ListBox.ItemTemplate>
                <DataTemplate DataType="...">
                    <Ellipse Width="10" Height="10"
                             Fill="White"
                             ToolTipService.ToolTip="{Binding Name}" />
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
    

    这很有魅力。

    现在,我想在 ToolTip 的图像。但是 工具提示 图片 只要有 列表框 在它的前面。

    我已经试着设置了 Background 列表框 Transparent {x:Null} ,我试图设置 背景 帆布 透明 {x:空} ,但到目前为止没有任何帮助。

    将来我可能想再放一个 列表框 在现有的一个之前显示其他额外的覆盖层,但恐怕第二个 列表框 我看不见 工具提示 第一个项目的s 列表框 我可能再也无法点击第一个项目了 列表框 .

    有什么建议吗?

    1 回复  |  直到 9 月前
        1
  •  1
  •   Clemens    9 月前

    即使有 Background="{x:Null}" 列表框会吞噬所有鼠标事件。您可以通过附加MouseMove事件处理程序来验证这一点。这肯定是由于列表框中的Border或ScrollViewer元素造成的 Template .

    因此,您必须修改模板,例如

    <ListBox ItemsSource="{Binding ListSource}" Background="{x:Null}">
        <ListBox.Template>
            <ControlTemplate TargetType="ListBox">
                <ItemsPresenter/>
            </ControlTemplate>
        </ListBox.Template>
        ...
    </ListBox>
    

    或者你只是使用 ItemsControl 而不是列表框:

    <Grid>
        <Image Source="{Binding ImageSource}"
               ToolTipService.ToolTip="{Binding ImageToolTip}" />
    
        <ItemsControl ItemsSource="{Binding ListSource}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <Canvas/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemContainerStyle>
                <Style TargetType="ContentPresenter">
                    <Setter Property="Canvas.Left" Value="{Binding Left}" />
                    <Setter Property="Canvas.Top" Value="{Binding Top}" />
                </Style>
            </ItemsControl.ItemContainerStyle>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Ellipse Width="10" Height="10" Fill="White"
                             ToolTipService.ToolTip="{Binding Name}" />
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Grid>