[Unity] How to use ToolTipOpening?
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.
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.
-
sfernandez
Site Admin
- Posts: 3093
- Joined:
Re: [Unity] How to use ToolTipOpening?
Hi,
As you need to manually add the handler to the event, you'll have to name the element first:
Then, you willl be able to find the element in the tree, and add your custom function:
Is this what you need?
As you need to manually add the handler to the event, you'll have to name the element first:
Code: Select all
<Rectangle x:Name="rect" ToolTip="Here is my ToolTip" .../>
Code: Select all
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...");
}
Re: [Unity] How to use ToolTipOpening?
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.
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...
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.
Code: Select all
<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>
.....
-
sfernandez
Site Admin
- Posts: 3093
- Joined:
Re: [Unity] How to use ToolTipOpening?
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:
And the xaml will be like this:
Does this solution make more sense in your scenario?
Code: Select all
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...");
}
}
Code: Select all
<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>
Re: [Unity] How to use ToolTipOpening?
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
And built the blend project.
and in my actual xamlHelper file
and lastly in my ToggleButton
and blend error is...
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
Code: Select all
xmlns:helper="clr-namespace:Assets.GloryAssets.Scripts.GUI.XamlHelpers"
and in my actual xamlHelper file
Code: Select all
#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
}
}
}
Code: Select all
<ToggleButton ... helper:ToolTipHelper.EnableToolTipHandler="true" .../>
Code: Select all
The attachable property 'EnableToolTipHandler' was not found in type 'ToolTipHelper'.
The member "EnableToolTipHandler" is not recognized or is not accessible.
-
sfernandez
Site Admin
- Posts: 3093
- Joined:
Re: [Unity] How to use ToolTipOpening?
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:
After adding these static methods it should work fine in Blend/Visual Studio.
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:
Code: Select all
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));
Re: [Unity] How to use ToolTipOpening?
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
and here is my Togglebutton xaml
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:
Unfortunately I did not manage to get this thing to work.
Here is my TooltipHelper class
Code: Select all
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
}
}
Code: Select all
<ToggleButton ... helper:ToolTipHelper.TooltipHandler="True" ... />
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:
Code: Select all
[dx9] Assets/GloryAssets/Cok_Xaml/Editors/UserControls/EquipmentEditorWindow.xaml
Ignoring unknown member Assets.GloryAssets.Scripts.GUI.XamlHelpers.ToolTipHelper.TooltipHandler (@219,104)
-
sfernandez
Site Admin
- Posts: 3093
- Joined:
Re: [Unity] How to use ToolTipOpening?
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:
And the xaml should look like this:
Change your code to the following:
Code: Select all
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
}
}
Code: Select all
<ToggleButton ... helper:ToolTipHelper.EnableTooltipHandler="True" ... />
Re: [Unity] How to use ToolTipOpening?
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..
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..
Code: Select all
<Button Margin="400" helper:ToolTipHelper.EnableToolTipHandler="True">
<Button.ToolTip>
<TextBox> what </TextBox>
</Button.ToolTip>
</Button>
-
sfernandez
Site Admin
- Posts: 3093
- Joined:
Re: [Unity] How to use ToolTipOpening?
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
Let me know if you have problems with that, otherwise use it in your project
- Attachments
-
- ToolTipTest.zip
- (8.09 KiB) Downloaded 201 times
Who is online
Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 4 guests