GridView 绑定源了一个ObserverCollection<Item> 集合 如何能够判定某一个item在当前视野中范围内？

• 问题

• 如题！绑定源了一个ObserverCollection<Item> 集合 如何能够判定某一个item在当前视野中范围内？

人生就要精彩

2013年4月27日 5:05

答案

• public static bool TestVisibility(this FrameworkElement item, FrameworkElement viewport, Orientation orientation, bool wantVisible)
{

// Determine the bounding box of the item relative to the viewport
GeneralTransform transform = item.TransformToVisual(viewport);
Point topLeft = transform.Transform(new Point(0, 0));
Point bottomRight = transform.Transform(new Point(item.ActualWidth, item.ActualHeight));

// Check for overlapping bounding box of the item vs. the viewport, depending on orientation
double min, max, testMin, testMax;
if (orientation == Orientation.Vertical)
{
min = topLeft.Y;
max = bottomRight.Y;
testMin = 0;
testMax = Math.Min(viewport.ActualHeight, double.IsNaN(viewport.Height) ? double.PositiveInfinity : viewport.Height);
}
else
{
min = topLeft.X;
max = bottomRight.X;
testMin = 0;
testMax = Math.Min(viewport.ActualWidth, double.IsNaN(viewport.Width) ? double.PositiveInfinity : viewport.Width);
}

bool result = wantVisible;

if (min >= testMax || max <= testMin)
result = !wantVisible;

return result;
}

2013年4月27日 6:42
• ???

2013年4月27日 8:07

全部回复

• public static bool TestVisibility(this FrameworkElement item, FrameworkElement viewport, Orientation orientation, bool wantVisible)
{

// Determine the bounding box of the item relative to the viewport
GeneralTransform transform = item.TransformToVisual(viewport);
Point topLeft = transform.Transform(new Point(0, 0));
Point bottomRight = transform.Transform(new Point(item.ActualWidth, item.ActualHeight));

// Check for overlapping bounding box of the item vs. the viewport, depending on orientation
double min, max, testMin, testMax;
if (orientation == Orientation.Vertical)
{
min = topLeft.Y;
max = bottomRight.Y;
testMin = 0;
testMax = Math.Min(viewport.ActualHeight, double.IsNaN(viewport.Height) ? double.PositiveInfinity : viewport.Height);
}
else
{
min = topLeft.X;
max = bottomRight.X;
testMin = 0;
testMax = Math.Min(viewport.ActualWidth, double.IsNaN(viewport.Width) ? double.PositiveInfinity : viewport.Width);
}

bool result = wantVisible;

if (min >= testMax || max <= testMin)
result = !wantVisible;

return result;
}

2013年4月27日 6:42
• ???

2013年4月27日 8:07