0

i have been created the new common class for ToolTip in Touchscreen. And i have a lot of the control and page for ToolTip. So i just want to using about this common class in XAML.

@ Common Class

public class CommonLayout : Window
{

    Timer Timer { get; set; }
    ToolTip toolTip { get; set; }

    public CommonLayout(TextBlock control)
    {
        Timer = new Timer();
        Timer.Interval = 3000;
        Timer.Elapsed += OnTimerElapsed;

        control.MouseLeave += OnMouseLeave;
        control.MouseLeftButtonUp += OnMouseLeftButtonUp;

    }

    public void OnMouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
    {
        CloseToolTip();
    }

    public void OnMouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        toolTip = ((ToolTip)((TextBlock)sender).ToolTip);
        toolTip.IsOpen = true;
        Timer.Start();
    }

    private void CloseToolTip()
    {
        if (toolTip != null)
        {
            toolTip.IsOpen = false;
        }
    }

    private void OnTimerElapsed(object sender, ElapsedEventArgs e)
    {
        Timer.Stop();
        Application.Current.Dispatcher.BeginInvoke((Action)CloseToolTip, DispatcherPriority.Send);
    }

}

@XAML i want to binding for common ToolTip class about below 3 kind of TextBlock. and it have to using in other page also.

<StackPanel Margin="30">
        <TextBlock x:Name="textBlock"   HorizontalAlignment="Left"  TextWrapping="Wrap" Text="ToolTip Test" 
                ToolTipService.ShowOnDisabled="True"   >
            <TextBlock.ToolTip>
                <ToolTip Placement="Mouse" Content="This is ToolTip Test." />
            </TextBlock.ToolTip>
        </TextBlock>

        <TextBlock x:Name="textBlock1"   HorizontalAlignment="Left"  TextWrapping="Wrap" Text="ToolTip Test1" 
                ToolTipService.ShowOnDisabled="True"   >
            <TextBlock.ToolTip>
                <ToolTip Placement="Mouse" Content="This is ToolTip Test."  />
            </TextBlock.ToolTip>
        </TextBlock>

        <TextBlock x:Name="textBlock2"   HorizontalAlignment="Left"  TextWrapping="Wrap" Text="ToolTip Test2" 
                ToolTipService.ShowOnDisabled="True"   >
            <TextBlock.ToolTip>
                <ToolTip Placement="Mouse" Content="This is ToolTip Test."  />
            </TextBlock.ToolTip>
        </TextBlock>

    </StackPanel>
Song
  • 11
  • 6

1 Answers1

0

Your CommonLayout is inherited from Window. So it can be used only as Windows, not as part of Window.

For your purpose it's more suit to use behaviours mechanism, it's especially for your case. Please follow to the next tutorial: https://wpftutorial.net/Behaviors.html

As result it will be looks like:

public class CommonLayout : Behavior<UIElement> {
   // your code with corrections from tutorial
}

Xaml is something like this:

<TextBlock>
 <e:Interaction.Behaviors>
    <b:CommonLayout/>
 </e:Interaction.Behaviors>
</TextBlock>

Don't forget add reference to assembly before: System.Windows.Interactivity

I hope it'll help you.

Alex Netrebsky
  • 251
  • 1
  • 7