I have a user control that I drag inside the grid. The Z-Index is set pretty high so I can keep it above the other kids. Dragging and dropping the control works fine, but if the user wants to move the control outside the grid, it will allow it.
How can I make it not leave the borders of the parent Grid control, here is what I have now:
private System.Windows.Point _anchorPoint; private System.Windows.Point _currentPoint; private bool _isInDrag; private void UserControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { var element = sender as FrameworkElement; _anchorPoint = e.GetPosition(null); if (element != null) element.CaptureMouse(); _isInDrag = true; e.Handled = true; } private void UserControl_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { if (!_isInDrag) return; var element = sender as FrameworkElement; if (element != null) element.ReleaseMouseCapture(); _isInDrag = false; e.Handled = true; } private void UserControl_MouseMove(object sender, MouseEventArgs e) { if (!_isInDrag) return; _currentPoint = e.GetPosition(null); UIElement container = VisualTreeHelper.GetParent(_parentGrid) as UIElement; System.Windows.Point relativeLocation = _parentGrid.TranslatePoint(new System.Windows.Point(0, 0), container); if (_currentPoint.X > relativeLocation.X) return; if(_currentPoint.Y >= relativeLocation.Y)return; _transform.X += _currentPoint.X - _anchorPoint.X; _transform.Y += (_currentPoint.Y - _anchorPoint.Y); RenderTransform = _transform; _anchorPoint = _currentPoint; }
"relativeLocation" is always 0x0, so it does not work. Any ideas would be greatly appreciated.
* Note. I know that if I changed my UserControl to Window, it would reduce all the problems that I have. But honestly, it looks great, and I really don't want to clutter up the window. This system opens as a control panel, which consumes the entire user window (opens in a separate window). Therefore, when you open a window here, it does not flow directly.
source share