Basically I need to know the ScrollViewer ScrollBar width.
This answer How to find Vertical Scrollbar width of a Scrollviewer in C#
doesn't work
SystemParameters.VerticalScrollBarWidth
Any clue?
I found solution here Changing the width of a vertical scrollbar
ScrollBar vertical = ((FrameworkElement)VisualTreeHelper.GetChild(scrollviewer1, 0)).FindName("VerticalScrollBar") as ScrollBar;
var w = vertical.Width;
Related
Is there a way to hide these arrows in a scrollbar of a scrollviewer?
I couldn't find anything on google on how to disable these, if that's not possible then is there a way to change their color?
You need to set the VerticalScrollBarVisibility property of your ScrollViewer. The options are
Auto - A ScrollBar appears and the dimension of the ScrollViewer is applied
to the content when the viewport cannot display all of the content.
For a horizontal ScrollBar, the width of the content is set to the
ViewportWidth of the ScrollViewer. For a vertical ScrollBar, the
height of the content is set to the ViewportHeight of the
ScrollViewer.
Disabled - A ScrollBar does not appear even when the viewport cannot display all of the content. The dimension of the content is set to the corresponding dimension of the ScrollViewer parent. For a horizontal ScrollBar, the width of the content is set to the ViewportWidth of the ScrollViewer. For a vertical ScrollBar, the height of the content is set to the ViewportHeight of the ScrollViewer.
Hidden - A ScrollBar does not appear even when the viewport cannot display all of the content. The dimension of the ScrollViewer is not applied to the content.
Visible - A ScrollBar always appears. The dimension of the ScrollViewer is applied to the content. For a horizontal ScrollBar, the width of the content is set to the ViewportWidth of the ScrollViewer. For a vertical ScrollBar, the height of the content is set to the ViewportHeight of the ScrollViewer.
When I create a new ScrollViewer, I need to modify the size of the ScrollBars in the ScrollViewer (change the VerticalScroll width and the HorizontalScroll height), programatically.
I tried the following based in a solution found in SO, without success:
public static ScrollViewer CreateScrollViewer()
{
ScrollViewer result = new ScrollViewer();
// I need to set the scroll width and height here
// I tried the following with no success
result.Resources.Add(SystemParameters.VerticalScrollBarWidth, 100);
return result;
}
I saw some solutions to change them, but all are XAML based. I need to do it in runtime, in pure C#. How could I do this?
You can access the ScrollBars from the ScrollViewer's ControlTemplate. You can find out how to do that from the How to: Find ControlTemplate-Generated Elements page on MSDN and you can find details of the default ControlTemplate in the ScrollViewer Styles and Templates page on MSDN, but in short, try this:
ScrollViewer scrollViewer = new ScrollViewer();
scrollViewer.ApplyTemplate();
ScrollBar scrollBar =
(ScrollBar)scrollViewer.Template.FindName("PART_VerticalScrollBar", scrollViewer);
You can do the same for the horizontal ScrollBar which is named PART_HorizontalScrollBar.
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.
How can I remove the small rectangle that appears when a horizontal and vertical scrollbar appear?
I assume you are using a ScrollViewer. The default template of a ScrollViewer is a Grid containing two rows and two columns. The upper left cell contains the ScrollViewer content, upper right the vertical ScrollBar, lower left the horizontal ScrollBar, and a Rectangle on the lower right cell. Please check this article: http://www.eggheadcafe.com/tutorials/aspnet/f51ddf8c-5227-4f1b-a5df-ec3d1b3439ca/styling-the-wpf-scrollviewer.aspx. You can see the default template there. You could remove the rectangle and then set either the ColumnSpan of the horizontal ScrollBar or RowSpan of the vertical ScrollBar to cover the lower right cell. Hope this helps.
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.