Change ScrollBar width in ScrollViewer programatically - c#

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.

Related

How to find the Scroll Bar width for ScrollerView in Silverlight

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;

Binding a TextBox's Height to Parent via Code

I have a TextBox that I'm creating, via code, to insert into a Control in a WPF interface. The Control can be dynamically resized via click-and-drag code, and I want the TextBox's height and width to match the Control's. The end result is that I have a TextBox that I can dynamically resize.
The box is going to be placed in this Border:
<Border Name="borInner" Margin="0,0,2,0" HorizontalAlignment="Center" VerticalAlignment="Center" />
I'm trying to make the textbox fill the area of the border. To do so, I'm assuming 2 things have to happen.
I have to set Horizontal and Vertical Alignments on borInner to Stretch
The TextBox must also have those properties.
Unfortunately, when both of these things are set, the TextBox matches the Width of the overall control, but not the height. It just sits centered vertically still. Does the Height value of a TextBox control just not follow Stretch? If I set a height manually, it will work, but I'd rather not have to manually update the size of the textbox during the mousemove if I can help it, as it's pretty slow to calculate what the new dimensions should be constantly.
For more detail on the Control the TextBox is being inserted into: It has a property I've made called bool FitContentToSize, which makes this code happen on the Control's creation
borInner.VerticalAlignment = VerticalAlignment.Stretch;
borInner.HorizontalAlignment = HorizontalAlignment.Stretch;
I then create the textbox
element = new TextBox() {
AcceptsReturn = true,
TextWrapping = TextWrapping.Wrap,
VerticalScrollBarVisibility = ScrollBarVisibility.Auto,
HorizontalAlignment = HorizontalAlignment.Stretch,
VerticalAlignment = VerticalAlignment.Stretch,
HorizontalScrollBarVisibility = ScrollBarVisibility.Auto
};
and add that TextBox to borInner.
What am I missing, or does anyone have a decent idea for a workaround?
Try setting the height of the textbox to Double.NaN and then setting the margin to 0 all around.
Make sure your inner border (borInner) is not inside something like a StackPanel, or anything else that limits the height of its children to the bare minimum. If this is the case, consider using something like a Grid, or DockPanel.

How to obtain visible borders of canvas placed on scrollviewer

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.

Make UserControl resize when Dock = Fill

I have a problem with UserControl that I'm crafting. It consists of TableLayoutPanel with another TableLayoutPanel in it with ListView inside. I want to make it resizable so that it will fit in left panel of my app and behave somewhat like Toolbox in Visual Studio. The problem is that my control doesn't scale when I resize panel.
UserControl is embedded in panel with Dock = Fill and Anchor = Tob, Left, Bottom, Right. Also all controls in it are made that way. How can I fix this?
EDIT: It's WinForms, not XAML.
Use a split panel and put your UserControl in the left panel and Dock.Fill it.
You're probably looking for the AutoSize properties on the TableLayoutPanel and the AutoSize ColumnType of that panel.
You can achieve something like a Dock = Fill by simply auto-sizing the table layout panel (GrowAndShrink) so that it will always fit your inner control.
Please post your designer code to see how you embedded the controls in which other control.
I suspect your resize problem come from your resizing strategy of control inside the TableLayoutPanel.
The table layout panel is tricky. Regarding the resize strategy you want to follow inside a cell of the table panel, the control in the cell have either to be Dock.Fill or Anchor = Top, Left, Bottom, Right.
Basically:
Il you want the grid cell to adapt to the size of the control, then have the control in the cell Anchor = Top, Left, Bottom, Right and set the row/column to autosize.
If you want the control in the cell to adapt to the cell size, use Dock.Fill on it and use a percentage or a absolute value to size your cell.
The behavior of the TableLayoutPanel is best described in the MSDN documentation.

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