там так :
| Код | this.Button.LongClick += new my.ButtonControl.LongClickEventHandler(Button_LongClick);
|
| Код | void Button_LongClick(object sender, my.ButtonControl.LongClickRoutedEventArgs e) { if (e.isLast) { this.Button.Visibility = Visibility.Hidden; } }
|
Добавлено @ 01:53 Извиняюсь за сокращение кода ((
Добавлено через 5 минут и 19 секунд Весь компонент кнопки моей:
| Код | namespace my { public partial class ButtonControl { Timer timer; private double _speedInterval = 10;
public double SpeedInterval { get { return this._speedInterval; } set { this._speedInterval = value; } } private object _ButtonLabel; public object ButtonLabel { get { Label i = this.Butt.Template.FindName("label", this.Butt) as Label; if (i != null && i.Content != null) return i.Content; return this._ButtonLabel; } set { Label i = this.Butt.Template.FindName("label", this.Butt) as Label; if (i != null) i.Content = value; this._ButtonLabel = value; } } public static readonly RoutedEvent LongClickEvent = EventManager.RegisterRoutedEvent( "LongClick", RoutingStrategy.Bubble, typeof(LongClickEventHandler), typeof(ButtonControl));
public class LongClickRoutedEventArgs : RoutedEventArgs { private TimeSpan _dur; private bool _last; public LongClickRoutedEventArgs(RoutedEvent routedEvent, object source, TimeSpan dur, bool last) : base(routedEvent, source) { _dur = dur; _last = last; }
public TimeSpan ClickDuration { get { return _dur; } }
public bool isLast { get { return _last; } } }
public event LongClickEventHandler LongClick { add { AddHandler(LongClickEvent, value); } remove { RemoveHandler(LongClickEvent, value); } } public delegate void LongClickEventHandler(object sender, LongClickRoutedEventArgs e); public ButtonControl() { this.InitializeComponent(); this.Butt.DataContext = this;
this.PreviewMouseDown += new MouseButtonEventHandler(ButtonControl_PreviewMouseDown); this.PreviewMouseUp += new MouseButtonEventHandler(ButtonControl_PreviewMouseUp); //this.LongClick += new LongClickEventHandler(ButtonControl_LongClick); }
void ButtonControl_LongClick(object sender, LongClickRoutedEventArgs e) { ///if(e.Dur.TotalSeconds>2) s.stopLongClick(); }
void ButtonControl_PreviewMouseUp(object sender, MouseButtonEventArgs e) { TimeSpan duration = DateTime.Now - downDate; ButtonControl bc = (ButtonControl)sender; bc.stopLongClick();
try { RaiseEvent(new LongClickRoutedEventArgs(ButtonControl.LongClickEvent, this, duration, true)); } catch { } } DateTime downDate; void ButtonControl_PreviewMouseDown(object sender, MouseButtonEventArgs e) { timer = new Timer(); downDate = DateTime.Now; timer.Interval = this._speedInterval; timer.Start(); timer.Elapsed += new ElapsedEventHandler(timer_Elapsed); } void stopLongClick() { timer.Stop(); timer.Close(); } void timer_Elapsed(object sender, ElapsedEventArgs e) { TimeSpan duration = DateTime.Now - downDate; if (duration.TotalMilliseconds < 200) { //return; } Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, new Action( delegate() { try { RaiseEvent(new LongClickRoutedEventArgs(ButtonControl.LongClickEvent, sender, duration, false)); } catch { } } ) ); } } }
|
|