using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Interactivity;
using System.Windows;

namespace FLY.UI.OSK
{
    
    public class KeyboardBehavior:Behavior<UIElement>
    {
        public static bool Enable = true;
        System.Windows.Input.MouseButtonEventHandler mouseButtonEventhandler;
        public KeyboardBehavior() 
        {
            mouseButtonEventhandler = new System.Windows.Input.MouseButtonEventHandler(AssociatedObject_MouseDown);
        }
        

        protected override void OnAttached()
        {
            base.OnAttached();

            AssociatedObject.MouseDown += mouseButtonEventhandler;
        }
        protected override void OnDetaching()
        {
            AssociatedObject.MouseDown -= mouseButtonEventhandler;
            base.OnDetaching();
        }
        void AssociatedObject_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (!Enable)
                return;
            DependencyObject dobj = sender as DependencyObject;
            //向上查找，直到是System.Windows.Controls.TextBox
            do
            {
                if (dobj is System.Windows.Controls.TextBox)
                {
                    break;
                }
                dobj=System.Windows.Media.VisualTreeHelper.GetParent(dobj);
            } while (dobj!=null);

            if (dobj != null)
            {
                System.Windows.Controls.TextBox tb = dobj as System.Windows.Controls.TextBox;
                if (tb.Tag is string)
                {
                    string s = tb.Tag as string;
                    if (s == "Full")
                    {
                        Window_FullPad w = new Window_FullPad();
                        w.Open(tb);
                        return;
                    }
                }
                
                {
                    Window_NumPad w = new Window_NumPad();
                    w.Open(tb);
                }
            }
        }
    }
   
}
