Samstag, 17. Juli 2010

Silverlight: Drag & Drop Timing problems

If you use the new Silverlight 4 controls for drag&drop you have sometimes the problem that in your control the drag Event started even if you only select the next item in your listbox or treeview. The reason for this is that you maybe have long running events at your SelectedItem-Events. This forces the Drag&Drop-Container to raise a DragEnter-Event because the control does not become the MouseUp-Event in the desired time.

To solve this problem we made in our current project several approaches (Background process etc.). But the best way to reach your goal is a very “oldschool” method:

Using a timer to make your long running events  in the SelectedItem-Event

On SelectedItem Changed:
   1: var selectedEntryChangedTimer =
   2:                     new DispatcherTimer
   3:                         {
   4:                             Interval =
   5:                                 new TimeSpan(0, 0, 0, 0,
   6:                                             250)
   7:                         };
   8:                 selectedEntryChangedTimer.Tick += OnSelectedEntryChangedTimer;
   9:                 selectedEntryChangedTimer.Start();
 
Eventhandler:
   1: private void OnSelectedEntryChangedTimer(object o, EventArgs args)
   2: {
   3:     var timer = (DispatcherTimer)o;
   4:     timer.Stop();
   5:  
   6:     EventBroker.GetEvent<SelectedBilanzObjektChangedEvent>().Publish(
   7:         new SelectedBilanzObjektChangedEventArgs(_selectedEntry));
   8: }

Keine Kommentare:

Kommentar veröffentlichen