private void Thumb_DragCompleted(object sender, DragCompletedEventArgs e) { //need to alter the drop code for new tilegrid behavior //before snapping back, see if we move to a new column/row DependencyObject parent = LogicalTreeHelper.GetParent(this); if (parent is TileGrid tg) { Tuple <int, int> rowCol = tg.GetRowColDimensions(new Size(tg.ActualWidth, tg.ActualHeight)); //use relative end positioning to panel rather than to start position. Point endPos = Mouse.GetPosition(tg); //we know where the thumb is but really we would like to know where THIS is Point thumbOffset = Mouse.GetPosition(this); endPos.X -= thumbOffset.X; endPos.Y -= thumbOffset.Y; //get the width and height of each row double colWidth = tg.ActualWidth / rowCol.Item2; double rowHeight = tg.ActualHeight / rowCol.Item1; //get row and column based on position of top-left corner. round off to an integer to snap to nearest int newCol = (int)Math.Round(endPos.X / colWidth); int newRow = (int)Math.Round(endPos.Y / rowHeight); //if the rowspan and columnspan would exceed the number of rows or columns, fix it newCol = Math.Min(rowCol.Item2 - TileGrid.GetColumnSpan(this), newCol); newRow = Math.Min(rowCol.Item1 - TileGrid.GetRowSpan(this), newRow); //snap to the correct row/column //this doesn't need to change TileGrid.SetColumn(this, newCol); TileGrid.SetRow(this, newRow); AlignAdorner(); //reset back to no margin //only do this if snapping; presumably we actually want to stay dragged otherwise Margin = defaultMargin; } }
public TileGridlineAdorner(TileGrid adornedElement) : base(adornedElement) { target = adornedElement; }
private void AlignAdorner() { //prefer the adorner to be on the left, but move to the right if we're in the first column menu.HorizontalAlignment = TileGrid.GetColumn(this) == 0 ? HorizontalAlignment.Right : HorizontalAlignment.Left; }