Make a TextBlock in a ScrollViewer Automatically Zoom to Fit - c#

I'm creating a Windows 8 Metro app. I have a TextBlock in a ScrollViewer. I'm wondering if it possible to programmatically change the ZoomFactor of the ScrollViewer to make the TextBlock auto-fit the width of the screen. Meaning, the longest line of the TextBlock fit the screen width (without wrapping).

Generally I find ViewBox to be the most useful way to scale xaml contents to fit a predetermined size. Simply leave your ScrollViewer and TextBlock with no size requirements (and turn wrapping off) and then put the ScrollViewer inside a ViewBox with Scale=Uniform (or possibly UniformToFit depending on your needs).
The TextBlock will cause the ScrollViewer to size appropriately for its contents, and then the ViewBox will scale the ScrollViewer to exactly fit in its own control extents. (Putting the ViewBox inside your root grid or as the root control of your page should cause it to fill the entire screen.)

Related

How to prevent TabItems stacking up vertically when window size is reduced?

As seen in this image, when I have a lot of tab items and resize the window so that the available space for tabitems is less than their width, the tab items start stacking up vertically. How do I prevent this?
You can change the style of your TabControl and substitute the TabPanel with another container according to your needs (for example a StackPanel with Orientation Horizontal).
The TabPanel can stack items if the width is less than desidered.

Making a StackPanel scrollable in Windows Phone 8

I have a list of controls, put into a stackpanel. These controls are Hubtiles, that are added programatically after a user creates it - a list essentially. I need to make the StackPanel in which they are added, scrollable - what would be the best approach for this? Should i put the stackpanel inside a ScrollView, and then increase the StackPanels height with the actual height of the Hubtile - to make it scrollable, but not so that the user can scroll infinite without something being there. So whenever the amount of controls inside the StackPanel reaches 3, it'll automatically increase it's height like this:
Whenever the amount of controls inside the StackPanel reaches 3 or above:
StackPanel.Height = StackPanel.Height + Hubtile.ActualHeight;
Thanks a lot!
Just put the StackPanel into ScrollViewer, set the StackPanel.VerticalAlignment to Stretch and set fixed size to the parent ScrollViewer. This is necessary - the ScrollViewer must know its size, to show the scrollbars for the inner content when the inner content is too long.

DataGrid auto sizing in WPF

I am dynamically creating data grids in a tab window in WPF. I am having an issue with the data grid sizes, that are within the tabControl.
This is not an issue of the size of data within the data grid but the size of data grid itself.
I am looking to set the height and width to auto. This is simple in XAML but I am struggling to find any information to do it in the code which I need to do..
I have tired double.NaN but this does not seem to work....
Any help would be amazing!
I am using c#.
Try
Height = new GridLength(1, GridUnitType.Auto);
The problem is probably the StackPanel. It won't give the controls inside of it any more space than they "need" in the direction of it's orientation (so a vertical stack panel will only give the minimum required vertical space to each element). Try replacing it with a Grid or DockPanel.
In other words - controls in a vertical StackPanel will NOT expand to fill available vertical space, similarly for a horizontal StackPanel.

How do I get the size of the visible part of a WPF usercontrol?

I have a usercontrol that consists of a label and a textbox. It's inside a scrollviewer.
I'm drawing an adorner on top of it and I need to adjust the size of the adorner to the visible size of the control.
How do I get the visible size of the control?
In the image below the green rectangle is the adorner. As you can see it's being drawn over the scrollbar on the right side.
Is it possible to get the size of the rendered part or will I have to manually go trough the visual tree and calculate it?
edit: more background info
I'm build a very limited form designer. Everything is happening in code. The adorner is used to display the current selected control.
I would put a Canvas in your ScrollViewer and place all of your user controls on the Canvas. If the Adorner is then painted on the Canvas you don't have to worry about it drawing over top your ScrollViewer.
You would also have the added benefit of the adorner disappearing under the ScrollViewer, rather than just ending at it, so your users know that the control extends beyond the ScrollViewer. This is how all of the designers I've made and seen made in WPF work.
If you are only worried about clipping the adorner, then you can include an AdornerDecorator in your content. Something like:
<ScrollViewer>
<AdornerDecorator>
<!-- Your content here -->
</AdornerDecorator>
</ScrollViewer>
Or you can include the AdornerDecorator directly in your UserControl.

How do I stretch the control to fill Canvas in Silverlight?

What I need to do is to stretch the control to fill entire Canvas.
I think I have to use a Canvas (I don't want my control to be clipped
when rendered outside the container - as far as I know all the other containers
perform clipping - Grid, Border... - maybe there is another solution?)
If I put the control inside Canvas it works fine (I mean it is not clipped).
However, it doesn't fill entire Canvas.
I was trying to bind to the Canvas width and height - with no results.
Do you have any ideas or clues?
Thanks in advance!
A control can escape the container such as a Border or a Grid by having negative margins. So perhaps you should return to using a Grid and play around with placement using column and row definitions and using negative margins where you need them.

Categories

Resources