How to relative scale size of User Control? - wpf

How to relative scale size of User Control like image (in image editor).
Example (100%):
(source: stegnar.com)
Scaled down UC (70%):
(source: stegnar.com)
Well I achieve this in picture editor, but I would like in WPF. :) I need this to adjust my application to different screen resolution, while nothing hiding (no scrollbars).

You could try the ViewBox control that scales up/down its content so that it fills the available space.
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
Title="Window1">
<Grid>
<Viewbox StretchDirection="Both" Stretch="Uniform">
<local:UserControl1 Height="600" Width="600"/>
</Viewbox>
</Grid>

you can place the whole container into a ViewBox
<Viewbox StretchDirection="Both" Stretch="Uniform">
<Grid>...</Grid>
</Viewbox>
you don't have to place each single textblock in it!

Use of Viewbox (as said by Milan Nankov) is a great idea. One thing that I must warn you is that it also zooms in or out other visual aspects as well.
For example, a Textbox with dimension 200 X 1000 is very different from a Textbox with dimension 20 X 100 zoomed in 10x.
WPF provides many layouting options which can change dimension of the controls according to the size of the container. But it doesn't change the size of the text. Viewbox overcomes this issue, but it introduces another issue. Check the image below which shows the same textbox in a viewbox before and after zooming.
One trick which could be used is to place every textblock in a viewbox. But I guess that would be an overkill, and I seriously don't having any backing for this trick. Please do check for yourself and reply whether it's practical or not.
Another trick could be to bind the control's height to the font size. We would be needing a converter in that case. Please refer to this reply.. Resize font in TextBox in Grid

Related

WPF what determines whether scrollviewer and stackpanel give infinite size to a child as opposed to a set size?

During Measure, I'm still really confused because this results in stackpanel's child (signalgraph) being given (292,Infinity) as the availableSize in Measure:
<Window x:Class="paneltesting.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wpfExp="clr-namespace:WpfExperimental"
Title="Window1" Height="300" Width="300">
<StackPanel
>
<wpfExp:SignalGraph/>
</StackPanel>
</Window>
While with a canvas of a set size the child is given (infinity, infinity) for its available size. I assume that this has something to do with an interaction with the window, but I'm really confused as to what is going on.
<Canvas Width="100" Height="100">
<StackPanel SizeChanged="ScrollViewer_SizeChanged"
>
<wpfExp:SignalGraph/>
</StackPanel>
</Canvas>
The same thing occurs when a scrollviewer is used. I just wanted to be able to understand how scrollviewer gives sizes to its children. I was wondering if changing the panels in its controltemplate might result in a scrollviewer that just passes the scrollviewer size as the availableSize to its child instead of infinite space.
I've already coded a work around, but I just want to understand this, so that I'm certain that there wasn't some much easier way to get the behavior I wanted.
StackPanel measures its children with infinity in the direction of its Orientation, so an infinite height when the Orientation is Vertical; the other orientation is usually given the constraint it got.
Canvas always measures its children with an infinite width and height.
For a ScrollViewer, if the Content implements IScrollInfo (i.e. it provides the scrolling functionality) and its CanContentScroll is true then the child is measured based on the constraint it is given, possibly minus the amount needed for the scrollbar(s) if they are shown. Of course then it is responsible for handling the scrolling.

WPF automatic resize font until it fits within parent control

I have a user control who's root element is a Grid.
I also have have a ContentControl that serves as a placeholder for some text that is filled when data is loaded. This control can be moved around within the user control and so it's position can be anywhere.
If the text is too long to fit within the root Grid of the user control, I want to reduce the font size of the ContentControl until the text fits.
My problem is that I can't seem to find an event that I can handle to do this process.
I tried using the ContentControl.LayoutUpdated event;however, the sender parameter for this always appears to be nothing...which is really not helpful!
I am really looking forward to any advise on how to achieve this.
Thank you
-Frinny
Wrapping your ContentControl in a Viewbox set to only scale down will do this for you:
<Viewbox StretchDirection="DownOnly" Stretch="Uniform">
<ContentControl Content="Some Text"/>
</Viewbox>

How to do Silverlight overlay element

I am starting to learn SL...
I am trying to make a MediaElement of size X, and on the bottom of the movie frame some subtitles that will run.
I was unable to understand if I need absolute position or something else.
Please advice
thanks
If you need to us it as a subtitle you just need to put your TextBlock under the MediaElement on your Grid and need to give VerticalAllignment property as bottom on XAML. And it will be over it. Like this;
<Grid>
<MediaElement/>
<TextBlock VerticalAllignment="Bottom"/>
</Grid>
You may refer Grid Layout as relative positioning if you're new to silverlight. And can give a margin to your textblock or anything you want just take a look at the intellisense (if using VS) and you'll understand, if you're using expression blend it'll be a lot easier with UI.
If you want to use absolute positioning you'll need to use Canvas instead of Grid Layout, its the same and you can change anything to canvas with nearly no problem. In canvas, you need to use left and right properties instead of allignments. Like this;
<Canvas>
<MediaElement/>
<TextBlock Canvas.Left="0" Canvas.Top="400"/>
</Canvas>
Another option is stackpanel its not really suitable for LayoutRoot, but its pretty nice for controls. So if you want your subtitles to stay under your movie you should use StackPanel like this;
<StackPanel Orientation="Vertical">
<MediaElement/>
<TextBlock/>
</StackPanel>
So to sum up;
-If you want your subtitles to be on top of your movie use grid like the first example,
-If you have a fixed size and you want to place your subtitles anywhere you want use Canvas,
-And if you want to put your subtitles under your movie use StackPanel.
-My personal choice would be grid. =)
For more information you may check this article it seems like a nice one!
http://weblogs.asp.net/scottgu/pages/silverlight-tutorial-part-2-using-layout-management.aspx
Happy coding!!
The TextBlock will overlay (within a Grid layout) the MediaElement simply because it is declared after the MediaElement. VerticalAlignment="Bottom" will place it at the bottom of the Grid. You might want to set the Grid's width and height (instead of the MediaElement) that of the size of the video. The MediaElement will auto size to stretch the full size of the grid.
<Grid x:Name="LayoutRoot" Width="480" Height="320">
<MediaElement/>
<TextBlock TextWrapping="Wrap" VerticalAlignment="Bottom"/>
</Grid>

WPF: view that scales to container at large sizes, scrolls at small sizes?

I am building a control depicting a diagram. The control's content (which is rather complex) will try to scale to fit allotted space to the extent possible. However, not all scales are valid. The content cannot shrink indefinitely. E.g. a box on a diagram should be at least 20 pixels wide. Thus, when the window is too small to fit the content even at the minimum size, scaling should stop and scroll bars must appear.
I cannot find an elegant solution for this in WPF. Any design ideas are greatly appreciated.
Set the Horizontal & VerticalAlignment of the content to Stretch but also set the MinWidth and MinHeight to appropriate values, place your content in a ScrollViewer whose Horizontal & VerticalScrollBarVisibility is set to Auto.
That should work, probably...
Example:
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
<Button
MinHeight="400"
MinWidth="400"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Content="Buttons!"/>
</ScrollViewer>
</Window>

Changing FontSize relationally to the windowsize with WPF?

Is it possible, that the FontSize getting smaller if I shrink the window and getting bigger if I enlarge the window?
Wrap your text inside a Viewbox.
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<Viewbox>
<TextBlock Text="Sizes to fit!"/>
</Viewbox>
</Window>
I've never tried this, but I imagine you can bind the font size property of your text to the window size through a converter method. I wouldn't try to bind directly, as that way madness lies.
The other option would be to handle the window resize events and send the font size to each control manually...
--edit--
Just searched Google and found this result, which may be what you want.

Resources