WPF Control form resize - wpf

I have two controls on my form, i have two use case
When i drag from the right corner, i want to re size the form and all controls should be aligned appropriately
When i drag from the right edge, the right side controls should be hidden
I have already tried the Res size mode as can re size and i can handle the first user scenario.
I have used a View Box property which is enabling the re size of window when i drag and shrink it.
I am not able to find a control that can allow me to re size and hide a portion of the window at two different events

Some ideas for you.
You may handle the SizeChanged event for your window.
SizeChangedEventArgs has two propertis: HeightChanged, WidthChanged.
Maybe you can collapse the viewbox when only Widthchanged is ture.
Update:
After tested, Collapse viewbox will collapse the content in viewbox. So I write code like below to avoid this issue. But It's worked not very well. Just FYI:
<Grid>
<Viewbox>
<content/>
</Viewbox>
<content/>
</Grid>

Related

Flexible XAML-only layout. Is it possible?

I want to create resizable window wich will initially autosize to its content.
If the size of the window reaches some limits autosizing is disabled and growing controls are either clipped or shown with scrollbars.
Autosizing also must be off when user resizes the window.
The real task is to create convinient resizable dialog window with text control.
When it contains not much and not few text lines it is reasonable to initially autosize the dialog. The amount of text increases and dialog becomes larger. Sure it must have some size constraints.
P.S. I think it's quite a frequent task to define the layout where the guiding role of the sizing during measure pass of the layout conditionaly swithces from children to parent and back.
Share you ideas or existing solutions. May be I'm missing something.
Thank you.
Update 1
Let me explain the algorithm:
1) Window is shown (let's imagine all data/content is already set).
2) Its size is adjusted as if window's properties were:
MaxHeight = ...
MaxWidth = ...
SizeToContent = "WidthAndHeight"
3) User tries to resize the window. And he can do it. Inner controls change accordingly their size.
This behavior is equivalent to the properties set:
MaxWidth= "{x:Ststic Double.PositiveInfinity}"
MaxHeight = "{x:Ststic Double.PositiveInfinity}"
SizeToContent = "Manual"
ResizeMode = "CanResizeWithGrip"
Yes, you can do it with XAML only using animations/triggers but it's a little tricky.
Just hook into TextBlock Loaded event(xaml only) and change SizeToContent=manual && MaxWidth&MaxHeight=PosInfinity inside EventTrigger using animation.
Many of WPF's panels automatically resize based on their children. I'd recommend looking through this article to get an idea of what kind of layout panels are available in WPF: WPF Layouts: A Visual Quick Start
But to answer your question, place all your controls you want automatically sized in a panel that automatically stretches its children, such as a Grid or a DockPanel, and set the MaxHeight and MaxWidth properties of the panel to prevent it from growing past a certain height/width.
<Grid MaxHeight="100" MaxWidth="200">
<!-- Place your content here -->
</Grid>
Depending on the default behavior of the parent control containing your panel, you may need to set the HorizontalAlignment and VerticalAlignment to something other than Stretch too.
<Grid>
<Grid MaxHeight="100" MaxWidth="200" HorizontalAlignment="Left" VerticalAlignment="Top">
<!-- Place your content here -->
</Grid>
</Grid>
If you want a ScrollBar instead of clipping the content, add a ScrollViewer inside the Grid

Panning contents of a Canvas in a ScrollViewer

I'm trying to implement panning within a Canvas within a scrollviewer like:
<ScrollViewer>
<Canvas>
<!-- some visual elements here -->
</Canvas>
</ScrollViewer>
I want a click and drag operation within the canvas to cause the contents of the canvas to move. I've tried handling the MouseDown, MouseMove, and MouseUp events to do a translation in the manner described here but it hasn't worked.
Any ideas?
You can't do that with your current setup. A Canvas will stretch beyond its parent container and the scrollviewer won't know the size of the Canvas (it will tell it it doesn't need to scroll) and therefore can't create the handles.
If you want to skip with that set up change the canvas to a grid and use the Vertical Scroll and Horizontal Scroll and associated set properties to move the visible section of the grid around.
Try giving your Canvas a set Width and Height and give it a background color (Transparent should be fine) and see if that helps you get your mouse events.

Scale usercontrol in wpf ? vertical and horizontal scrollbars

I implemented a usercontrol, but i have a problem with rescaling the window. I know when i make the window smaller, everything scales, and every textbox and label becomes also smaller. But this is not what i want, i just want that when i make the screen smaller, everything stays the same size, and that scrollbars appear ( vertical and horizontal ). How do i do that?
Thanks
Assumption
The behaviour you describe, is not mandatory the default layout-behaviour of WPF. It depends on the layout-controls you use. I assume, you're using a Grid with setting it's columns and rows to the Start (*)-GridLengths. This would have more or less such an effect as you describe (without scaling). Or maybe you are using a ViewBox, this control scales the whole content based on the available layout size.
Solution
I guess that wrapping your whole content into a ScrollViewer will probably do what you desire. If not, I suggest that you post some XAML-code to show us how you have built your content.
<ScrollViewer>
<YourContent>
</YourContent>
</ScrollViewer>
Update
If you really scale your window (applying a ScaleTransformation) and you want your UserControl to be the only control within that does not scale, you have to scale your UserControl in the opposite direction as you have done it with your window. Apply a ScaleTransformation and set the scale values to 1/scale. Or try to use the ViewBox to blow up the content of your UserControl, but this will not be very exact.

How to create a UserControl with an irregular shape?

In my Silverlight 4 application I need to create a user control with an irregular shape. The "main display" of the UC is a standard rectangle but I need to have tabs (simple text blocks, where the user can click) that are outside of the main display rectangle.
Is this possible with Silverlight 4? If so, how?
Thanks in advance.
You can position elements of a control outside its normal layout in a number of ways. You could use Canvas but if most of the control is standard Grid rectangle then you can use a Grid. The trick is to use negative Margins.
<Grid x:Name="LayoutRoot">
<Border Margin="0 -22 0 0">
<TextBlock Text="I appear above the UserControl layout" />
</Border>
</Grid>
Note that if the Usercontrol is being used as the Visual root then this won't work because the Silverlight plugin will not render beyound its client rectangle.
It is, you can have transparent background behind the tabs which can let clicks through, effectively behaving as if the shape was different. The UserControl will still have a rectangular shape including the tabs, unless you wrap then into a Popup and float out of the UC with some offset.
Technically, you can have elements outside the UserControl's rectangle if you use a Canvas for your LayoutRoot instead of a Grid. Elements in a Canvas aren't clipped to the canvas size. I wouldn't recommend this, however, because you won't be able to use Margin to size and align your controls inside it. It would be better to have all child controls inside a Grid LayoutRoot.
Which brings us to the question of irregularity. If you want to 'see through' parts of the control and be able to click through them (i.e. click objects underneath it), all you need to do is keep the UserControl's and the LayoutRoot's Background to null or just not set it at all. Wherever there is a lack of any background, clicks will go through. Note that if you set the background to Transparent it will make the control behave as a rectangle (as if it's filled with solid color) with respect to mouse input.
Another thing is if you want to see HTML controls under the see-through parts of your app. Then, you'll have to use windowless mode, but that's another can of worms.

Stretching a WPF Canvas Horizontally

How do I make a Canvas stretch fully horizontally with variable width? This is the parent Canvas, so it has no parents, only children.
XAML Source: it displays in blend
http://resopollution.com/xaml.txt
Use a Grid as the top level element in your UI - it'll stretch to fill its container. Then put a Canvas with HorizontalAlignment="Stretch" inside the Grid and it'll behave the way you want.
<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Canvas Background="Blue"/>
</Grid>
That worked for me. The key is your top level UI element. While a Grid fills all available space by default, Canvases take up only as much room as their contents demand.
I'm guessing you've tried
canvas.HorizontalAlignment = HorizontalAlignment.Stretch
If this doesn't work, then what you could do is bind the Width and Height properties of the canvas to the ActualWidth and ActualHeight properties of the containing window.
You could use a dock panel to get it to fill the available width. The last item in a dock panel list of controls is automatically stretched to fill the remaining space.
<DockPanel>
<Canvas />
</DockPanel>
The canvas should do this automatically, unless you are manually setting the height and/or width. What kind of control are you trying to place the canvas on? Can you post your code?
The problem is that you're specifying the Height and Width. Without these properties, the control may appear to vanish in the designer, but it should size appropriately when you insert the canvas into another control.
If I recall correctly, the next version of WPF will have 'DesignWidth' and 'DesignHeight' properties that allow you to show the control in the designer with a given size without effecting it's measurement when inserted into other controls.

Resources