WPF绑定到工具提示

16

不确定这里出了什么问题,但是绑定在数据模板中的标签可以工作,而工具提示则不行。非常感谢任何帮助。

                    <DataTemplate DataType="Label">
                    <StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
                        <StackPanel.ToolTip>
                            <ToolTip DataContext="{Binding Path=PlacementTarget,
       RelativeSource={x:Static RelativeSource.Self}}">
                                <TextBlock Text="{Binding Path=DataContext.Description}" />
                            </ToolTip>
                        </StackPanel.ToolTip>
                        <Image Source="{StaticResource ApplicationInfoS}" 
                               Margin="0 0 5 0" Stretch="None"
                               HorizontalAlignment="Left" />
                        <Label Style="{StaticResource lblTextContent}" 
                               Padding="5 0 0 0"
                               Content="{Binding Path=DataContext.Description, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}}"/>
                    </StackPanel>
                </DataTemplate>

顺便提一句,DataTemplate是在ListView中使用的。 "Description" 属性存在于绑定到ListView的视图模型上。

我在VS2010的输出窗口中得到的消息如下:

System.Windows.Data Error: 39 : BindingExpression路径错误:'Description'属性在 'object' ''String' (HashCode=-466763399)' 上未找到。BindingExpression:Path=DataContext.Description; DataItem='StackPanel'(名称=''); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')

更新

我暂时放弃了。暂时使用以下Hack:

向StackPanel添加一个标签,将“Description”绑定到它上面。

    <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" Tag="{Binding Path=DataContext.Description, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}}">

将ToolTip绑定到标签。 是一种hack但它可行。

<StackPanel.ToolTip>
  <ToolTip DataContext="{Binding Path=PlacementTarget, RelativeSource={x:Static RelativeSource.Self}}">
    <TextBlock Text="{Binding Path=Tag}" />
  </ToolTip>
</StackPanel.ToolTip>

干杯

Mike

4个回答

6

提示工具不应需要相对源绑定。请尝试使用无数据上下文绑定的方法。

<StackPanel.ToolTip>
    <ToolTip Content={Binding Description} />
<StackPanel.ToolTip>

假设此DataTemplate基于的Label类型具有名为Description的属性。在某些情况下,您可能需要绑定到PlacementTarget的是ContextMenu控件。


此描述来自与该视图相关联的ViewModel。 - ozczecho
数据模板是基于ViewModel还是其他类? - TerrorAustralis

3

工具提示是一个弹出窗口。有时需要在主窗口范围之外显示,因此工具提示不能位于主可视树中。如果查看其可视树层次结构,您会发现弹出窗口有自己的可视树根。这就是为什么工具提示不能自动了解列表项的DataContext的原因。现在的问题是如何将DataContext传播到工具提示的可视树中。您在更新中演示的方法是其中一种方法...


1

我在工具提示的绑定方面也遇到了问题,因为工具提示被定义为资源。我通过为ToolTipOpening事件创建事件处理程序来解决了这个问题。在处理函数中,您可以访问显示的UI元素的DataContext并设置Tooltip的DataContext。

这是我的XAML:

<StackPanel ToolTip=" " ToolTipOpening="Item_ToolTipOpening" >

这是我的代码处理程序:

void Item_ToolTipOpening(object sender, ToolTipEventArgs e)
    {
        if (sender as FrameworkElement == null)
            return;
        ToolTip tooltip = (ToolTip) FindResource("MailItemToolTip");
        if ((sender as FrameworkElement).DataContext is LinkItem)
            tooltip.DataContext = ((sender as FrameworkElement).DataContext as LinkItem).ParentItem as MailItem;
        else if ((sender as FrameworkElement).DataContext is AttachmentItem)
            tooltip.DataContext = ((sender as FrameworkElement).DataContext as AttachmentItem).ParentItem as MailItem;
        (sender as FrameworkElement).ToolTip = tooltip;
    }

请注意,必须设置ToolTip(至少设置一些值),否则将不会调用ToolTipOpening事件。

0
如果这个DataTemplate是像你所说的那样作为ListView的ItemTemplate,那么你不需要在ToolTip上设置DataContext,也不需要在Label.Content绑定中使用RelativeSource BindingExpression。 DataContext应该已经被ListView设置了。这就是我们使用DataTemplate的原因。
尝试只使用常规绑定即可:
<DataTemplate>
    <StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
        <StackPanel.ToolTip>
            <ToolTip>
                <TextBlock Text="{Binding Path=Description}" />
            </ToolTip>
        </StackPanel.ToolTip>
        <Image Source="{StaticResource ApplicationInfoS}" 
               Margin="0,0,5,0"
               Stretch="None"
               HorizontalAlignment="Left" />
        <Label Style="{StaticResource lblTextContent}" 
               Padding="5,0,0,0"
               Content="{Binding Path=Description}" />
    </StackPanel>
</DataTemplate>

不,这不是ItemTemplate的DataTemplate。这是Listview中列的DataTemplate。DataTemplate在资源中定义为样式。 - ozczecho

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接