0

I get this runtime error:

Unable to cast object of type 'System.Windows.Controls.ToolTip' to type System.Windows.Controls.Label. I understand what that means but I'm not sure why it's happening.

when I mouse over a treeviewitem. What I want is the ToolTipOpening on a treeview item to run a method. Here is the XAML. I did this based on on this thread:

TreeViewItem Tooltip Binding Not Working

It wasn't quite my problem but it did tell me at least how to set the tooltip on a treeview item.:

 <TreeView x:Name="ISLTreeView" Height="auto" Background="GhostWhite" 
                       BorderThickness="0" Width="auto"
                       ItemsSource="{Binding}" 
                        ScrollViewer.VerticalScrollBarVisibility="auto">

                <TreeView.ItemContainerStyle>
                    <Style TargetType="{x:Type TreeViewItem}">
                        <Setter Property="IsExpanded" 
                           Value="{Binding IsExpanded, Mode=TwoWay}" />
                        <Setter Property="ToolTip">
                            <Setter.Value>
                                <ToolTip  
                                        ToolTipOpening="ISL_TreeViewTipOpening">
                                </ToolTip>
                            </Setter.Value>
                        </Setter>
                        <Style.Triggers>
                            <Trigger Property="IsSelected" Value="True">
                                <Setter Property="FontWeight" Value="Bold" />
                            </Trigger>

                        </Style.Triggers>
                    </Style>
                </TreeView.ItemContainerStyle>

            </TreeView>

Here is the method, there's not much in it yet.

  private void ISL_TreeViewTipOpening(object sender, ToolTipEventArgs e)
    {
        TreeListViewItem tvi = sender as TreeListViewItem;

    }

Edited to add the next XAML code that defines a Label and another treeview

 <Label x:Name="NeighborLabel" Content="Neighbors List"></Label>
            <TreeView x:Name="NeighborsTreeView" Height="Auto" 
                      Background="GhostWhite" BorderThickness="0" Width="auto" 
                      ItemsSource="{Binding}"
                      ScrollViewer.VerticalScrollBarVisibility="auto">

            </TreeView>
Community
  • 1
  • 1
David Green
  • 1,210
  • 22
  • 38
  • Are you sure the exception comes from this xaml? – Newton Zou Nov 08 '16 at 05:47
  • yes. the exception occurs on the line that sets up the ToolTipOpening and is a XAMLParseException with the message that 'SetConnectionID threw an Exception'. – David Green Nov 08 '16 at 12:25
  • not sure if this should be in a comment or an answer, but I found this thread on stackoverflow: http://stackoverflow.com/questions/6970706/xamlparseexception-on-window-launch which directed me to look at the .g.i.cs file for my window class and I find this: – David Green Nov 08 '16 at 12:30
  • #line 104 "..\..\..\SwitchBrowser.xaml" this.NeighborLabel.ToolTipOpening += new System.Windows.Controls.ToolTipEventHandler(this.ISL_TreeViewTipOpening); so the event handler is being attached to a label. I don't know why. I've edited the original question to include that XAML code also. – David Green Nov 08 '16 at 12:36

2 Answers2

0

According to the description on codeproject.com, the TreeListViewItem class inherits from ListViewItem. Your sender parameter is a TreeViewItem. Perhaps you meant to cast to type TreeViewItem.

Peter S
  • 66
  • 2
  • It never gets that far. The ISLTreeView is part of a window. The exception gets thrown during initialization of the window - the window never opens. I can post all the XAML if I need to. – David Green Nov 08 '16 at 04:27
0

I figured out that the ToolTipOpening can't be attached to the TreeviewItem. It was getting attached to the label because that was the first Control that the compiler found that the event could be attached to, and then I was getting the invalid cast. When I added a TextBox to the treeivew item and attached the event handler to it, ToolTipOpening event was handled properly without an exception.

David Green
  • 1,210
  • 22
  • 38