未验证 提交 ff05a4be 编写于 作者: P punker76

(#346) Try to fix getting the wrong descendant bounds of an item...

(#346) Try to fix getting the wrong descendant bounds of an item (TreeViewItem) with our own GetVisibleDescendantBounds version

Credits and idea goes to @Metritutus
上级 3b16e068
......@@ -57,7 +57,7 @@ private static DragDropPreview GetDragDropPreview(DropInfo dropInfo, UIElement t
if (useVisualSourceItemSizeForDragAdorner)
{
var bounds = VisualTreeHelper.GetDescendantBounds(dragInfo.VisualSourceItem);
var bounds = VisualTreeExtensions.GetVisibleDescendantBounds(dragInfo.VisualSourceItem);
itemsControl.SetValue(FrameworkElement.MinWidthProperty, bounds.Width);
}
......@@ -78,7 +78,7 @@ private static DragDropPreview GetDragDropPreview(DropInfo dropInfo, UIElement t
if (useVisualSourceItemSizeForDragAdorner)
{
var bounds = VisualTreeHelper.GetDescendantBounds(dragInfo.VisualSourceItem);
var bounds = VisualTreeExtensions.GetVisibleDescendantBounds(dragInfo.VisualSourceItem);
contentPresenter.SetValue(FrameworkElement.MinWidthProperty, bounds.Width);
contentPresenter.SetValue(FrameworkElement.MinHeightProperty, bounds.Height);
}
......
......@@ -4,6 +4,8 @@
namespace GongSolutions.Wpf.DragDrop
{
using GongSolutions.Wpf.DragDrop.Utilities;
public class DropTargetHighlightAdorner : DropTargetAdorner
{
public DropTargetHighlightAdorner(UIElement adornedElement, DropInfo dropInfo)
......@@ -28,17 +30,19 @@ protected override void OnRender(DrawingContext drawingContext)
var tvItem = visualTargetItem as TreeViewItem;
if (tvItem != null && VisualTreeHelper.GetChildrenCount(tvItem) > 0)
{
var descendant = VisualTreeHelper.GetDescendantBounds(tvItem);
var descendant = VisualTreeExtensions.GetVisibleDescendantBounds(tvItem);
var translatePoint = tvItem.TranslatePoint(new Point(), this.AdornedElement);
var itemRect = new Rect(translatePoint, tvItem.RenderSize);
descendant.Union(itemRect);
translatePoint.Offset(1, 0);
rect = new Rect(translatePoint, new Size(descendant.Width - translatePoint.X - 1, tvItem.ActualHeight));
}
if (rect.IsEmpty)
{
rect = new Rect(visualTargetItem.TranslatePoint(new Point(), this.AdornedElement), VisualTreeHelper.GetDescendantBounds(visualTargetItem).Size);
rect = new Rect(visualTargetItem.TranslatePoint(new Point(), this.AdornedElement), VisualTreeExtensions.GetVisibleDescendantBounds(visualTargetItem).Size);
}
drawingContext.DrawRoundedRectangle(null, this.Pen, rect, 2, 2);
}
}
......
......@@ -52,7 +52,7 @@ public static bool DirectlyOverElement(this Point dropPosition, UIElement elemen
var relativeItemPosition = element.TranslatePoint(new Point(0, 0), relativeToElement);
var relativeDropPosition = new Point(dropPosition.X - relativeItemPosition.X, dropPosition.Y - relativeItemPosition.Y);
return VisualTreeHelper.GetDescendantBounds(element).Contains(relativeDropPosition);
return VisualTreeExtensions.GetVisibleDescendantBounds(element).Contains(relativeDropPosition);
}
/// <summary>
......@@ -91,18 +91,19 @@ private static BitmapSource CaptureScreen(Visual target, FlowDirection flowDirec
}
var bounds = VisualTreeHelper.GetDescendantBounds(target);
var cropBounds = VisualTreeExtensions.GetVisibleDescendantBounds(target);
#if NET461 || NET46 || NET452 || NET451 || NET45
var dpiX = DpiHelper.DpiX;
var dpiY = DpiHelper.DpiY;
var dpiBounds = DpiHelper.LogicalRectToDevice(bounds);
var dpiBounds = DpiHelper.LogicalRectToDevice(cropBounds);
#else
var dpiScale = VisualTreeHelper.GetDpi(target);
var dpiX = dpiScale.PixelsPerInchX;
var dpiY = dpiScale.PixelsPerInchY;
var dpiBounds = DpiHelper.LogicalRectToDevice(bounds, dpiScale.DpiScaleX, dpiScale.DpiScaleY);
var dpiBounds = DpiHelper.LogicalRectToDevice(cropBounds, dpiScale.DpiScaleX, dpiScale.DpiScaleY);
#endif
var pixelWidth = (int)Math.Ceiling(dpiBounds.Width);
......@@ -118,6 +119,10 @@ private static BitmapSource CaptureScreen(Visual target, FlowDirection flowDirec
using (var ctx = dv.RenderOpen())
{
var vb = new VisualBrush(target);
vb.ViewportUnits = BrushMappingMode.Absolute;
vb.Viewport = bounds;
if (flowDirection == FlowDirection.RightToLeft)
{
var transformGroup = new TransformGroup();
......
......@@ -22,6 +22,7 @@ public static UIElement TryGetNextAncestorDropTargetElement(this UIElement eleme
{
return null;
}
var ancestor = element.GetVisualAncestor<UIElement>();
while (ancestor != null)
{
......@@ -29,8 +30,10 @@ public static UIElement TryGetNextAncestorDropTargetElement(this UIElement eleme
{
return ancestor;
}
ancestor = ancestor.GetVisualAncestor<UIElement>();
}
return null;
}
......@@ -70,6 +73,7 @@ public static T GetVisualAncestor<T>(this DependencyObject d)
{
return itemAsT;
}
item = VisualTreeHelper.GetParent(item);
}
......@@ -97,11 +101,13 @@ public static DependencyObject GetVisualAncestor(this DependencyObject d, Type i
return currentVisual;
}
}
if (itemContainerSearchType.IsAssignableFrom(currentVisualType))
{
// ok, we found an ItemsControl (maybe an empty)
return null;
}
currentVisual = VisualTreeHelper.GetParent(currentVisual);
}
......@@ -125,12 +131,14 @@ public static DependencyObject GetVisualAncestor(this DependencyObject d, Type i
{
return lastFoundItemByType;
}
var currentVisualType = currentVisual.GetType();
if ((currentVisualType == itemSearchType || currentVisualType.IsSubclassOf(itemSearchType))
&& (itemsControl.ItemContainerGenerator.IndexFromContainer(currentVisual) != -1))
{
lastFoundItemByType = currentVisual;
}
currentVisual = VisualTreeHelper.GetParent(currentVisual);
}
......@@ -165,5 +173,19 @@ public static IEnumerable<T> GetVisualDescendents<T>(this DependencyObject d)
yield break;
}
public static Rect GetVisibleDescendantBounds(Visual visual)
{
var bounds = VisualTreeHelper.GetDescendantBounds(visual);
if (visual is UIElement uiElement)
{
var cropBounds = new Rect(bounds.Location, new Size(bounds.Width, uiElement.DesiredSize.Height));
return cropBounds;
}
return bounds;
}
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册