WPF hint that works with touch screen

I have a WPF application where the tooltip is attached to the viewmodel and everything is fine when I took control and waited for the tooltip.

To support mouseless devices, I would like to show the same tooltip when I “click” on my control.

If I implement a MouseDown handler that sets the tooltip IsOpen = true, a tooltip is displayed, but the binding was not evaluated unless I find and wait first.

XAML:

<Window x:Class="TooltipApplication.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:TooltipApplication"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Image Source="http://www.brockmann-consult.de/beam/doc/help/visat/images/icons/Help22.png" MouseDown="Image_MouseDown" Stretch="None" MouseLeave="Image_MouseLeave">
            <Image.ToolTip>
                <ToolTip>
                    <TextBlock Text="{Binding Hint}"/>
                </ToolTip>
            </Image.ToolTip>
        </Image>

    </Grid>
</Window>

Code behind:

using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace TooltipApplication
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;
        }

        public string Hint { get { return "This is my hint"; } }


        private void Image_MouseDown(object sender, MouseButtonEventArgs e)
        {
            var tooltip = (sender as FrameworkElement).ToolTip;
            if (tooltip is ToolTip)
                ((ToolTip)tooltip).IsOpen = true;
        }

        private void Image_MouseLeave(object sender, MouseEventArgs e)
        {
            var tooltip = (sender as FrameworkElement).ToolTip;
            if (tooltip is ToolTip)
                ((ToolTip)tooltip).IsOpen = false;
        }
    }
}

Using the code above, click on the image before the tooltip timer expires, or run it on the touch device.

Hint , , .

, ?

+4

Source: https://habr.com/ru/post/1617250/


All Articles