Etana
Topic Author
Posts: 17
Joined: 21 Aug 2016, 13:14

[Unity] How to use ToolTipOpening?

27 Oct 2016, 21:09

Hi,
I have difficult time trying to understand how do I use ToolTipOpening of frameworkelement. I know Noesis for unity does not support code behind so is there currently any solution to get my custom function called whenever tooltip is open?
Thanks in advance.
 
User avatar
sfernandez
Site Admin
Posts: 2984
Joined: 22 Dec 2011, 19:20

Re: [Unity] How to use ToolTipOpening?

28 Oct 2016, 19:14

Hi,

As you need to manually add the handler to the event, you'll have to name the element first:
<Rectangle x:Name="rect" ToolTip="Here is my ToolTip" .../> 
Then, you willl be able to find the element in the tree, and add your custom function:
void Start()
{
  var gui = GetComponent<NoesisGUIPanel>();
  var content = gui.GetContent();
  var rect = (Rectangle)content.FindName("rect");
  rect.ToolTipOpening += Rect_ToolTipOpening;
}

private void Rect_ToolTipOpening(object sender, ToolTipEventArgs e)
{
  Debug.Log("ToolTip is opening...");
} 
Is this what you need?
 
Etana
Topic Author
Posts: 17
Joined: 21 Aug 2016, 13:14

Re: [Unity] How to use ToolTipOpening?

29 Oct 2016, 17:52

Thanks for your reply.
Yeah I knew I should have elaborate a bit more my current use case.
So in my case I can't add handler to my event (or can I?) because I use datatemplate to make my elements
here is a snippet of my xaml.
<ItemsControl ItemsSource="{Binding Path=MyLastingEffect.LastingEffectCollection}" >
<ItemsControl.ItemTemplate>
<DataTemplate>
      <RadioButton Margin="2" GroupName="Modifiers">
          <RadioButton.Template>
              <ControlTemplate>
                  <Border Width="50" Tag="{Binding ButtonID}" >
                      <ToggleButton ToolTipOpening="ModifierBonus_ToolTipOpening" 
                    IsChecked="{Binding IsChecked, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
                    ContextMenuService.IsEnabled="false">  
                    <TextBlock Text="{Binding Tag, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Border}}}"/>
                          <ToggleButton.ContextMenu>
<ContextMenu IsOpen="{Binding RelativeSource={RelativeSource TemplatedParent},Path=IsChecked}">
<MenuItem Header=".../>
<MenuItem Header=".../>
                              </ContextMenu>
                          </ToggleButton.ContextMenu>
                          <ToggleButton.ToolTip>
                          .....
because x:name can't be binded I can't that easily just add handler from code, but ofc I'm not xaml wizard so I might be wrong... :roll:
 
User avatar
sfernandez
Site Admin
Posts: 2984
Joined: 22 Dec 2011, 19:20

Re: [Unity] How to use ToolTipOpening?

31 Oct 2016, 16:49

Ok, in your scenario I think it is better to use an attached property to add the handler easier. This attached property could be defined in the code behind of the root UserControl, or as helper class:
public class ToolTipHelper: DependencyObject
{
  public static DependencyProperty EnableToolTipHandlerProperty = DependencyProperty.Register(
    "EnableToolTipHandler", typeof(bool), typeof(ToolTipHelper),
    new PropertyMetadata(false, OnEnableToolTipHandler));

  private static void OnEnableToolTipHandler(object sender, DependencyProperttyChangedEventArgs e)
  {
    FrameworkElement element = sender as FrameworkElement;
    if (element != null)
    {
      element.ToolTipOpening += OnToolTipOpening; // this could be a handler from any known instance
    }
  }

  private static void OnToolTipOpening(object sender, ToolTipEventArgs e)
  {
    Debug.Log("ToolTip is opening...");
  }
} 
And the xaml will be like this:
<DataTemplate>
      <RadioButton Margin="2" GroupName="Modifiers">
          <RadioButton.Template>
              <ControlTemplate>
                  <Border Width="50" Tag="{Binding ButtonID}" >
                      <ToggleButton local:ToolTipHelper.EnableToolTipHandler="true"
                        IsChecked="{Binding IsChecked, Mode=TwoWay,
                          RelativeSource={RelativeSource TemplatedParent}}"
                        ContextMenuService.IsEnabled="false">
     ...
</DataTemplate> 
Does this solution make more sense in your scenario?
 
Etana
Topic Author
Posts: 17
Joined: 21 Aug 2016, 13:14

Re: [Unity] How to use ToolTipOpening?

31 Oct 2016, 19:29

Thanks for your message.
I tried your approach but I failed either because my little knowledge of what I'm doing or your example did not work.
So I added this to my UserControl to the top
xmlns:helper="clr-namespace:Assets.GloryAssets.Scripts.GUI.XamlHelpers"
And built the blend project.
and in my actual xamlHelper file
#if UNITY_STANDALONE || UNITY_ANDROID || UNITY_IOS || UNITY_WINRT_8_1
#define UNITY
#endif

#if UNITY
using Noesis;
using UnityEngine;
#else
using System.Windows.Data;
using System.Windows;
using System.Windows.Controls;
#endif
using System;
using System.Globalization;

namespace Assets.GloryAssets.Scripts.GUI.XamlHelpers
{
    public class ToolTipHelper : DependencyObject
    {
        public static DependencyProperty EnableToolTipHandlerProperty = DependencyProperty.Register(
          "EnableToolTipHandler", typeof(bool), typeof(ToolTipHelper),
          new PropertyMetadata(false, OnEnableToolTipHandler));

        private static void OnEnableToolTipHandler(object sender, DependencyPropertyChangedEventArgs e)
        {
            FrameworkElement element = sender as FrameworkElement;
            if (element != null)
            {
                element.ToolTipOpening += OnToolTipOpening; // this could be a handler from any known instance
            }
        }

        public static void OnToolTipOpening(object sender, ToolTipEventArgs e)
        {
            #if UNITY
            Debug.Log("ToolTip is opening...");
            #endif
        }
    }
}
and lastly in my ToggleButton
<ToggleButton ... helper:ToolTipHelper.EnableToolTipHandler="true" .../>
and blend error is...
The attachable property 'EnableToolTipHandler' was not found in type 'ToolTipHelper'.
The member "EnableToolTipHandler" is not recognized or is not accessible.
 
User avatar
sfernandez
Site Admin
Posts: 2984
Joined: 22 Dec 2011, 19:20

Re: [Unity] How to use ToolTipOpening?

04 Nov 2016, 10:39

Hi,

Sorry, the code I attached only works in Noesis. In WPF attached properties also require a static getter and setter for the property. If you type 'propa' in Blend or Visual Studio it will provide you a code snippet for an attached property that looks like this:
public static int GetMyProperty(DependencyObject obj)
{
    return (int)obj.GetValue(MyPropertyProperty);
}

public static void SetMyProperty(DependencyObject obj, int value)
{
    obj.SetValue(MyPropertyProperty, value);
}

// Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
public static readonly DependencyProperty MyPropertyProperty =
    DependencyProperty.RegisterAttached("MyProperty", typeof(int), typeof(ownerclass), new PropertyMetadata(0)); 
After adding these static methods it should work fine in Blend/Visual Studio.
 
Etana
Topic Author
Posts: 17
Joined: 21 Aug 2016, 13:14

Re: [Unity] How to use ToolTipOpening?

07 Nov 2016, 23:04

Hi Sfernandes and thanks a lot for helping with this.

Unfortunately I did not manage to get this thing to work.
Here is my TooltipHelper class
public class ToolTipHelper : DependencyObject
    {
        public static bool GetTooltipHandler(DependencyObject obj)
        {
            return (bool)obj.GetValue(EnableToolTipHandlerProperty);
        }
        public static void SetTooltipHandler(DependencyObject obj, bool value)
        {
            obj.SetValue(EnableToolTipHandlerProperty, value);
        }

        public static DependencyProperty EnableToolTipHandlerProperty = DependencyProperty.RegisterAttached(
          "EnableToolTipHandler", typeof(bool), typeof(ToolTipHelper),
          new PropertyMetadata(true, OnEnableToolTipHandler));

        private static void OnEnableToolTipHandler(object sender, DependencyPropertyChangedEventArgs e)
        {
            FrameworkElement element = sender as FrameworkElement;
            if (element != null)
            {
                element.ToolTipOpening += OnToolTipOpening; // this could be a handler from any known instance
            }
        }

        public static void OnToolTipOpening(object sender, ToolTipEventArgs e)
        {
#if UNITY
            Debug.Log("ToolTip is opening...");
#endif
        }
    }
and here is my Togglebutton xaml
<ToggleButton ... helper:ToolTipHelper.TooltipHandler="True" ... /> 
And the result is...nothing, no error, no warnings, nothing.
I placed some break points in my TooltipHelper class but nothing ever gets fired.

Do you have any idea what could be the problem here?

Edit:
Actually there is one warning generated by Noesis:
[dx9] Assets/GloryAssets/Cok_Xaml/Editors/UserControls/EquipmentEditorWindow.xaml
Ignoring unknown member Assets.GloryAssets.Scripts.GUI.XamlHelpers.ToolTipHelper.TooltipHandler (@219,104)
 
User avatar
sfernandez
Site Admin
Posts: 2984
Joined: 22 Dec 2011, 19:20

Re: [Unity] How to use ToolTipOpening?

10 Nov 2016, 19:47

Looking at your code I see that you provide a different name to the dependency property "EnableToolTipHandler", than the one used in the static functions and the xaml "ToolTipHandler".

Change your code to the following:
    public class ToolTipHelper : DependencyObject
    {
        public static bool GetEnableToolTipHandler(DependencyObject obj)
        {
            return (bool)obj.GetValue(EnableToolTipHandlerProperty);
        }
        public static void SetEnableToolTipHandler(DependencyObject obj, bool value)
        {
            obj.SetValue(EnableToolTipHandlerProperty, value);
        }

        public static DependencyProperty EnableToolTipHandlerProperty = DependencyProperty.RegisterAttached(
          "EnableToolTipHandler", typeof(bool), typeof(ToolTipHelper),
          new PropertyMetadata(true, OnEnableToolTipHandler));

        private static void OnEnableToolTipHandler(object sender, DependencyPropertyChangedEventArgs e)
        {
            FrameworkElement element = sender as FrameworkElement;
            if (element != null)
            {
                element.ToolTipOpening += OnToolTipOpening; // this could be a handler from any known instance
            }
        }

        public static void OnToolTipOpening(object sender, ToolTipEventArgs e)
        {
#if UNITY
            Debug.Log("ToolTip is opening...");
#endif
        }
    }
And the xaml should look like this:
<ToggleButton ... helper:ToolTipHelper.EnableTooltipHandler="True" ... />
 
Etana
Topic Author
Posts: 17
Joined: 21 Aug 2016, 13:14

Re: [Unity] How to use ToolTipOpening?

11 Nov 2016, 08:54

Thanks for your answer,
That was indeed really silly mistake by me which I think I accidentally left there when I tried all kind of different solutions to this problem.

So even after changing that line to how it should be there is still no methods fired when tooltip is open.
I made for this just a simple new xaml with only one grid and a button element in there, button xaml is..
            <Button Margin="400" helper:ToolTipHelper.EnableToolTipHandler="True">
                <Button.ToolTip>
                    <TextBox> what </TextBox>
                </Button.ToolTip>
            </Button>
 
User avatar
sfernandez
Site Admin
Posts: 2984
Joined: 22 Dec 2011, 19:20

Re: [Unity] How to use ToolTipOpening?

11 Nov 2016, 12:22

Please find attached a Unity project with a working sample of this ToolTipOpening hook. You need to import the NoesisGUI unity package so it can compile and then play.

Let me know if you have problems with that, otherwise use it in your project ;)
Attachments
ToolTipTest.zip
(8.09 KiB) Downloaded 131 times

Who is online

Users browsing this forum: Bing [Bot] and 95 guests