开发者

WPF Binding to Tooltip

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.

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

BTW the DataTemplate is used in Listview. "Description" property exists on the view model bound to the list view.

The message I get in the output window in VS2010 is:

System.Windows.Data Error: 39 : BindingExpression path error: 'Description' property not found on 'object' ''String' (HashCode=-466763399)'. BindingExpression:Path=DataContext.Description; DataItem='StackPanel' (Name=''); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')

UPDATE

I have given up for now. Using the following hack for the time being:

Add a Tag to the StackPanel and Bind "Description" to it

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

Bind the ToolTip to the Tag. Yes a hack but it works.

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

Cheers

Mike


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.


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...


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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜