I'm creating a simple App, in which I instantiate elements of a custom class in runtime (the class consists of some Control elements: Textblocks, Grids). Since my working area could easily be overpopulated, I want the main Grid of my App scrollable.
I encapsulated my Grid in a Scroll Viewer, and I intend to increase the Width and the Height of the Grid this way making the Horizontal and Vertical Scrollbars appear. My problem is, however, that I don't know how to check if the instance of my custom class is entirely within the working area, or parts of it stretch out of it. I found a method in SO that could solve my problem HERE, but because I have many elements, I would have to check every single one of them. Is there another method that would go easy on performance?
Also, I have to know to what extent my element goes beyond the area, so that I only increase the size of the grid accordingly. I'm new to WPF, so any suggestions are much appreciated!
Here is my idea schematically:
Here is the relevant part of my XAML:
<ScrollViewer VerticalScrollBarVisibility="Visible"
HorizontalScrollBarVisibility="Auto"
CanContentScroll="True">
<Grid
x:Name="MainGrid"
Background="Transparent"
MouseLeftButtonUp="grid_MouseLeftButtonUp">
<ItemsControl>
<!--HERE I HAVE A NUMBER OF OTHER ELEMENTS PREDEFINED-->
</ItemsControl>
<Grid.ContextMenu>
<!--HERE I HAVE CONTEXTMENU ELEMENTS-->
</Grid.ContextMenu>
</Grid>
</ScrollViewer>