2012-07-11 23 views
5

soy nuevo con WPF. Tengo una ventana de WPF con una cuadrícula de datos que inicia un proceso cuando se produce un doble clic. Esto funciona muy bien, pero cuando hago esto en una tableta (con Windows 7), usando la pantalla táctil, el proceso nunca ocurre. Entonces necesito emular el evento de doble clic con eventos táctiles. ¿Alguien puede ayudarme a hacer esto, por favor?Emular el evento de doble clic en Datagrid con touchDown

Respuesta

0

Ver How to simulate Mouse Click in C#? de cómo emular un clic del ratón (en Windows formas), pero funciona en WPF haciendo:

using System.Runtime.InteropServices; 

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

    [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] 
    public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo); 

    private const int MOUSEEVENTF_LEFTDOWN = 0x02; 
    private const int MOUSEEVENTF_LEFTUP = 0x04; 
    private const int MOUSEEVENTF_RIGHTDOWN = 0x08; 
    private const int MOUSEEVENTF_RIGHTUP = 0x10; 

    public void DoMouseClick() 
    { 
     //Call the imported function with the cursor's current position 
     int X = //however you get the touch coordinates; 
     int Y = //however you get the touch coordinates; 
     mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0); 
    } 
} 
} 
+0

¡Muchas gracias! – sonseiya

+0

@somseiya No hay problema –

0

Primero se debe agregar una función de ratón evento click:

/// <summary> 
/// Returns mouse click. 
/// </summary> 
/// <returns>mouseeEvent</returns> 
public static MouseButtonEventArgs MouseClickEvent() 
{ 
    MouseDevice md = InputManager.Current.PrimaryMouseDevice; 
    MouseButtonEventArgs mouseEvent = new MouseButtonEventArgs(md, 0, MouseButton.Left); 
    return mouseEvent; 
} 

Añadir un evento de clic a uno de sus controles WPF:

private void btnDoSomeThing_Click(object sender, RoutedEventArgs e) 
{ 
    // Do something 
} 

Por último, llamar al evento clic desde cualquier función:

btnDoSomeThing_Click(new object(), MouseClickEvent()); 

Para simular un doble clic, añadir un evento de doble clic como PreviewMouseDoubleClick y asegurarse de que cualquier código comienza en una función separada:

private void lvFiles_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e) 
{ 
    DoMouseDoubleClick(e); 
} 

private void DoMouseDoubleClick(RoutedEventArgs e) 
{ 
    // Add your logic here 
} 

Para invocar el evento de doble clic, simplemente llamarlo desde otra función (como KeyDown):

private void someControl_KeyDown(object sender, System.Windows.Input.KeyEventArgs e) 
{ 
    if (e.Key == Key.Enter) 
     DoMouseDoubleClick(e); 
} 
Cuestiones relacionadas