WPF Binding to Tooltip

前端 未结 4 1348
误落风尘
误落风尘 2021-02-13 01:37

Not sure whats doing here, but the binding works for the label in the data template but not the tool tip. Any help will be appreciated.

                    

        
相关标签:
4条回答
  • 2021-02-13 01:58

    I also had problems with bindings in the tooltip, since the tooltip was defined as a resource. I solved the problem by creating an event handler for ToolTipOpening event. In the handler function you can then access the DataContext of the displayed UI element and set the DataContext of the Tooltip.

    This was my XAML:

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

    and this was my code handler:

    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;
        }
    

    Note that the ToolTip has to be set (at least to some value), otherwise the ToolTipOpening event is not called.

    0 讨论(0)
  • 2021-02-13 02:00

    If this DataTemplate is the ItemTemplate for a ListView like you state, then you shouldn't need to set the DataContext on the ToolTip nor have a RelativeSource BindingExpression in your Label.Content binding. The DataContext should already be set by the ListView. That's why we use the DataTemplate.

    Try just a regular binding for each:

    <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>
    
    0 讨论(0)
  • 2021-02-13 02:01

    Tooltip should not need a relativesource binding. Try this with no data context binding

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

    This is assuming that the Label type that this DataTemplate is based on has a property called Description. One of the few instances where you may need to bind to the PlacementTarget is in some cases with a ContextMenu control.

    0 讨论(0)
  • 2021-02-13 02:03

    Tooltip is a popup. Sometimes it needs to display out side of your main window bounds. So tooltip can't be in your main visual tree. If you look at its visual tree hierarchy, you will find that popup has its own visual tree root. That's why tooltip isn't aware of the list item's DataContext automatically. Now the question becomes how to propagate the DataContext to tooltip's visual tree. The way you demonstrated in your UPDATE is one way of doing it...

    0 讨论(0)
提交回复
热议问题