How to obtain visible borders of canvas placed on scrollviewer - c#

I have canvas which is placed on scrollviewer in order to allow scrolling. Is it possible to get coordinates of visible part of canvas?
I was trying to calculate it that way
leftBorder = ScrollViewer1.HorizontalOffset;
rightBorder = ScrollViewer1.ViewportWidth - ScrollViewer1.HorizontalOffset;
topBorder = ScrollViewer1.VerticalOffset;
bottomBorder = ScrollViewer1.ViewportHeight - ScrollViewer1.VerticalOffset;
but it seems that it is not working.

The Horizontal and Vertical offset is the actual scroll value in that direction.
Besides that, if you want the size of the content without any scrollbars that might be visible.
You can search for the child named "PART_ScrollContentPresenter". This shows the actual content of the scrollviewer, and this content will be resized when the scrollbars needs more space.
Hope that helps.

Related

auto sliding of Scrollbars

In my Winforms application, i have a User Control which serves as a 'screen' to draw various 2D shapes.
i have set its 'AutoScroll' property to true, and scrollbars works fine when you zoom the screen( i.e. User control)
Now, when i select any shape ( like rectangle or circle etc) and move it so that it goes beyond visible part of screen, i want respective scroll bars to auto slide in order to keep that shape on the visible area of screen.
do i need to set any other property of scrollbar ??
I don't think it is possible to achieve that without creating your own method.
You can set your scrollbar positon with:
this.VerticalScroll.Value = Y;
Then you have to find out the position of your Rectangle via:
Rectangle.Location.Y;
So this should work for your vertical scrollbar:
this.VerticalScroll.Value = Rectangle.Location.Y;
horzontal:
this.HorizontalScroll.Value = Rectangle.Location.X;
Combined with a MouseDown-Event it will do the trick.
Take a look here at the MSDN documention on exactly what the AutoScroll property is and does. It simply will enable the container to have a virtual size that is larger than its visible boundaries. It doesn't actually do the scrolling for you.
If you want the control to "move" with the user as they drag a shape, you will have to capture that action on your own and manually scroll the control over. I'd suggest starting with the MouseDown and MouseMove events. You'll need some logic to figure out when scrolling is needed and how much to actually scroll.

Horizontally scroll a fixed header/footer

I have a header that is set at a minimum width of 960px and is fixed to the top of the page. When the user shrinks the window to a size smaller than 960px, the horizontal scrollbar appears, as it should. However, when the user scrolls horizontally, since some of the header is cut off, I would like to be able to scroll the header at the same rate as the page... I pretty much want my header fixed vertically, but not horizontally.
I've looked at the following, but no help:
CSS: fixed position on x-axis but not y?
Centering a fixed element, but scroll it horizontally
I know this is a dated question and am not sure if you figured it out yet, but wanted to submit an answer in case someone else has the same problem.
When you set content to fixed in CSS with a minimum width, it is locked relative to the window on both axes. When the window is resized to less than minimum width, the excess header/footer is clipped and cannot be scrolled to. Unfortunately, there is no way to change this behavior in CSS alone. There is, however a simple jQuery fix for your problem (assuming you already have other content on the page that will create the horizontal scroll bar):
$(window).scroll(function ()
{
$("header,footer").css('margin-left', -($(window).scrollLeft()) + "px");
});
This will cause the header and footer (as appropriate) to move on the x axis in the opposite direction of the scroll, thus achieving your desired effect.

Estimate position and Width/Height of rendered UserControl

I have a UserControl (boxes) that can have varying size based on the number of items in its ItemsControl.
Many such usercontrols are added to a Canvas programmatically.
I need to draw arrows interconnecting these usercontrols. What is the best way to get the origin coordinates of the control w.r.t the Canvas and the rendered Width/Height so that I can figure out the arrow start and endpoints.
Canvas provides the coordinates of each control via Canvas.Left and Canvas.Top attached properties - which you know if you positioned them yourself anyway. So the (slightly) harder part is getting the other coordinate, and for that you want to know the rendered height/width. ActualHeight and ActualWidth give you this, assuming the control has already been laid out:
double top = Canvas.GetTop(control)
double bottom = top + control.ActualHeight
double left = Canvas.GetLeft(control)
double right = left + control.ActualWidth
If you're doing this before the controls have had a chance to be rendered on the screen, you can first do control.UpdateLayout() (or control.Measure()) to ensure the layout system measures their size.

Make vertical ScrollViewer invisible

I have a ScrollViewer control in my window and I would like to hide it when the user cannot scroll down the page. Just like with the horizontal scroll viewer. here is an example:
so here both scrollers are visible because scrolling is enabled:
if I collapse some of the columns of my listview note how eventually the horizontal scroller disappears:
Now note what happens with the vertical scroller:
so far its visible and that's ok because not all the content fits in the page. But let me maximize the page and co-lapse all the group boxes so that all the content fits in the page:
Why is it visible if it is not possible to scroll? I set all the group boxes height = 0 except the last one and the vertical scroller still shoes up? The horizontal scroller disappeared when it was not possible to scroll any more. Why does the vertical scroller does not behave the same way?
What can I do to make it invisible when it is not possible to scroll?
Make sure that your Scrollviewer's VerticalScrollBarVisibility = ScrollBarVisibility.Auto;

Determine the width of the vertical scroll bar in a ScrollViewer

I'm using a ScrollViewer to display an Image. The Image has a ScaleTransform set as one of it's LayoutTransforms. I've got it setup to fit the width of the image into the ActualSize of the ScrollViewer. My problem is that if the image height requires the vertical scrollbar to be present (I have it set to Auto) then my image is scaled just a little bit to much. I know how to determine if the scrollbar would be present and how to get the correct scale, but I cannot figure out how to determine what the actual width of the scrollbar is. I guess I could just guess at it, but I'd like something that would work if I later add styles to my application that would result in the scrollbars being a different size. Additionally I'm also doing Fit to Height and would need to get the Height of the horizontal scrollbar when it would be visible (I'm assuming that the answer to getting the width of the vertical scrollbar would make getting the height of the horizontal scrollbar obvious).
You can use SystemParameters.ScrollWidth.
Using ViewableHeight and ViewableWidth instead of ActualHeight and ActualWidth in my scaling calculations along with setting the scroll bars Visibility to Visible instead of Auto works. However I'll accept another answer that allows the scroll bars to be set to Auto instead.
Edit:
OK, I've now got the scroll bars set to Visible. Then I do my calculation with the ViewableHeight and ViewableWidth. Then I set the scroll bars back to Auto. This seems to work even if it's not all that elegant.

Categories