-
- darthmaule2
- Posts: 95
- Joined:
Any way to know if a UIElement is visible in the window?
I have a ListView bound to an ObservableCollection of 100s of items, each of which has an associated Noesis.BitmapImage that must be displayed as a thumbnail on the screen. I can't load them all at once or the UI thread pauses for a long time. So, I only want to load the ones that are visible in the screen when scrolling stops.
How do I check if a UIElement is visible? (The IsVisible property returns true even for offscreen elements).
I found the below code somewhere but it uses "TransformBounds" which doesn't appear to be implemented in Noesis...
Thanks.
How do I check if a UIElement is visible? (The IsVisible property returns true even for offscreen elements).
I found the below code somewhere but it uses "TransformBounds" which doesn't appear to be implemented in Noesis...
Code: Select all
public static bool IsUserVisible(this UIElement element)
{
if (!element.IsVisible)
return false;
var container = VisualTreeHelper.GetParent(element) as FrameworkElement;
if (container == null) throw new ArgumentNullException("container");
Rect bounds = element.TransformToAncestor(container).TransformBounds(new Rect(0.0, 0.0, element.RenderSize.Width, element.RenderSize.Height));
Rect rect = new Rect(0, 0, container.ActualWidth, container.ActualHeight);
return rect.IntersectsWith(bounds);
}
Thanks.
-
-
sfernandez
Site Admin
- Posts: 2056
- Joined:
Re: Any way to know if a UIElement is visible in the window?
Is the BitmapImage exposed as a property in each item view model?
Considering a ListView uses virtualization to render the items (so only visible ones are generated in the UI side), you can maybe delay the loading of the images to the first time the Image property is requested:
Do you think this could work for you?
Considering a ListView uses virtualization to render the items (so only visible ones are generated in the UI side), you can maybe delay the loading of the images to the first time the Image property is requested:
Code: Select all
public class Item
{
public Noesis.BitmapImage Image
{
get
{
if (_image == null)
{
_image = new Noesis.BitmapImage(_imagePath);
}
return _image;
}
}
...
}
Re: Any way to know if a UIElement is visible in the window?
I'm creating a VirtualizingWrapPanel and I need this function. Is there any work around?
-
-
sfernandez
Site Admin
- Posts: 2056
- Joined:
Re: Any way to know if a UIElement is visible in the window?
Are you referring to TransformBounds function?
We are deviating from WPF because our TransformToAncestor returns a 4x4 matrix, but you can use the returned matrix to transform a Rect as follows:
We are deviating from WPF because our TransformToAncestor returns a 4x4 matrix, but you can use the returned matrix to transform a Rect as follows:
Code: Select all
Rect bounds = new Rect(0.0, 0.0, element.RenderSize.Width, element.RenderSize.Height);
bounds.Transform(element.TransformToAncestor(container));
-
-
sfernandez
Site Admin
- Posts: 2056
- Joined:
Re: Any way to know if a UIElement is visible in the window?
Just to make it clear, we don't have TransformBounds implemented, but you can implement it yourself with the aforementioned code:
In the next release we will add it to Matrix4 type so following code will also work in Noesis:
Code: Select all
public static class Matrix4Ext
{
public static Rect TransformBounds(this Matrix4 matrix, Rect bounds)
{
bounds.Transform(matrix);
return bounds;
}
}
Code: Select all
Rect bounds = element.TransformToAncestor(container).TransformBounds(new Rect(0.0, 0.0, element.RenderSize.Width, element.RenderSize.Height));
Who is online
Users browsing this forum: No registered users and 2 guests