2 Replies - 4028 Views - Last Post: 17 March 2013 - 06:25 PM Rate Topic: -----

#1 kirabitoy101   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 08-November 11

Drag ListBoxItem to another ListBox of another TabPage - WPF

Posted 13 March 2013 - 07:40 AM

The code that I attached below is for dragging the ListBoxItem to another ListBox and it's working pretty well. However,what I wanted to happen is that I will be able to drag a ListBoxItem and transfer it to another ListBox which is contained by another TabPage.
The problem that I encounter is that during the drag event, I could not open another TabPage for me to able to drop the currently dragged item to the ListBox it contains.

	private void ListBox_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
		{

		ListBox parent = (ListBox)sender;
		dragSource = parent;
		object data = GetDataFromListBox(dragSource, e.GetPosition(parent));

		if (data != null)
		{
			DragDrop.DoDragDrop(parent, data, DragDropEffects.Move);
		}
		}

		private void ListBox_Drop(object sender, DragEventArgs e)
		{
			ListBox parent = (ListBox)sender;
			object data = e.Data.GetData(typeof(string));
			((IList)dragSource.ItemsSource).Remove(data);
			parent.Items.Add(data);
		}
	

		private void inventoryCharacterTabControl_DragOver(object sender, DragEventArgs e)
		{
			System.Windows.Point position = e.GetPosition(this);
			double pointX = position.X;
			double pointY = position.Y;
			Point pt = new Point(pointX, pointY);
			
			pt = PointFromScreen(pt);
			pt.Y = 18;
			// this locks in the Y value
			// you are only concerned with the X position
			TabControl TC = inventoryCharacterTabControl;
			int TP = 0;
			TP = GetDataFromTab(TC, pt);
			TC.SelectedIndex = TP;
			TC.Focus();
		}

		private int GetDataFromListBox(ListBox source, Point point)
		{
			UIElement element = source.InputHitTest(point) as UIElement;
			int tabIndex = 0;
			if (element != null)
			{
				object data = DependencyProperty.UnsetValue;
				while (data == DependencyProperty.UnsetValue)
				{
					data = source.ItemContainerGenerator.ItemFromContainer(element);

					if (data == DependencyProperty.UnsetValue)
					{
						element = VisualTreeHelper.GetParent(element) as UIElement;
					}

					tabIndex = inventoryCharacterTabControl.Items.IndexOf(data);
					if (element == source)
					{
						return tabIndex;
					}
				}

				if (data != DependencyProperty.UnsetValue)
				{
					return tabIndex;
				}
			}

			return tabIndex;
		}

		private int GetDataFromTab(TabControl source, Point point)	
		{
			UIElement element = source.InputHitTest(point) as UIElement;
			int tabIndex = 0;
			if (element != null)
			{
				object data = DependencyProperty.UnsetValue;
				while (data == DependencyProperty.UnsetValue)
				{
					data = source.ItemContainerGenerator.ItemFromContainer(element);
					
					if (data == DependencyProperty.UnsetValue)
					{
						element = VisualTreeHelper.GetParent(element) as UIElement;
					}

					tabIndex = inventoryCharacterTabControl.Items.IndexOf(data);
					if (element == source)
					{
						return tabIndex;
					}
				}
				
				if (data != DependencyProperty.UnsetValue)
				{
					return tabIndex;
				}
			}

			return tabIndex;
		}


Is This A Good Question/Topic? 0
  • +

Replies To: Drag ListBoxItem to another ListBox of another TabPage - WPF

#2 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Drag ListBoxItem to another ListBox of another TabPage - WPF

Posted 13 March 2013 - 07:55 AM

View Postkirabitoy101, on 13 March 2013 - 08:40 AM, said:

The problem that I encounter is that during the drag event, I could not open another TabPage for me to able to drop the currently dragged item to the ListBox it contains.


Right. So what's your question?





Presumably it's something to the effect of "Can someone think of a way around this for me?"

You might consider a handler for the .MouseHover event on the tabs of a TabPage. In the event of mouseHover make that page the forwards/selected page programmatically so the user can continue the drag event.
Was This Post Helpful? 0
  • +
  • -

#3 kirabitoy101   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 08-November 11

Re: Drag ListBoxItem to another ListBox of another TabPage - WPF

Posted 17 March 2013 - 06:25 PM

thanks tlhIn`toq for your reply. I've tried what you've said but still it doesn't work.

I used the MouseEnter event because I haven't seen the MouseHover event in WPF. When I started to drag the ListBoxItem to another TabPage it still doesn't open the Tab. It shows a cancel button when I hovered to the next TabPage.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1