The "visible area" of a control? - wpf

I have a Canvas within a ScrollViewer. The Canvas have a size of 600x600 and the ScrollViewer
400x400. If i scroll to the right side, i can't see 200pxl left side of the Canvas. Therefore my area of Canvas begins at x=200 but actually the visible area at x=0. Is there any way to get so "visile area" of a control by a property or something like that ?
Regards
Gepro

What are you looking for?
Choose the one from the list below:
ContentVerticalOffset
ContentHorizontalOffset
ViewportWidth
ViewportHeight

Related

Crop WPF control

I have to crop some control to show only a half of it but the rest should be transparent and clickable so it is not enough to cover the control with something. The result should give a control with only half of the content (for example 50% of top) and the rest should be cropped (not hidden) so some other control below should be visible and not overlapped by cropped part. New control should also scale when window is scaled. How to do this in WPF?
I have finally did the trick using Border around the control and Clip property of this border was set to Multibinging that was generating Rectangle basing on ActualWidth and ActualHeight of my control
Maybe GridSplitter:
http://www.wpf-tutorial.com/panels/gridsplitter/
Can be used to split views horizontally/vertically, and can be responsive.

Windows forms app, autoscale controls with form

I'm a newbie. Designing a form that can be resized, and I want my textboxes, labels and buttons to resize with the form, can someone tell me how to do this?
It depends on the type of layout you need. The "basic tools" you have to do that are following properties: Anchor and Dock.
Anchor
With the Anchor property you "attach" a side of an element to a side of its container. For example if you place a button in the bottom-right corner of a window and you set "Bottom, Right" as Anchor then when you'll resize the form the button will keep its relative position to that corner.
Now imagine you place a multiline text-box in the form, resize as needed (for example 4 px from top, left and right border and 128 px height) and set the Anchor property to "Left, Top, Right". When you'll resize the form that control will keep its height but it'll resize to keep its margins (so if you'll make the form wider its width will be increased).
Dock
Dock is different. With docking you "say" to the Layout Manager to use all available space in one direction. For example if you set to Left then your control will keep its width but it'll use all the available height and its location will be most left as possible.
You may have more than one control docked in a container, imagine you have 5 textbox with Top docking inside a form. They'll be stacked to the top of the form using all the width (and resizing). Another example: a Top docked control (as a banner) and a "Fill" docked control (as main content). Remember that with docking the order of controls matters (if you first place the "Fill" control it'll use ALL the available space and the "Top" dock control will overlap).
Even more
Moreover you have some layout controls too (tables and stacks). They're really easy to use and a 30 minutes of "experiments" will clarify much better than a long text.

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.

Panel with percentage coordinates

I would like use a panel whose children have coordinates specified as percentage of total panel's width/height. Moreover, I should be able to animate the coordinate property, for example to make a button move from 10% to 50% panel's width.
I've made 2 attempts:
Use a Grid and specify size as stars - this was not enough, because AFAIK by default WPF cannot animate distance properties specified by stars. I've found somewhere a custom class that enabled me to do so, it even worked, hovewer I consider that solution overly complicated an I am looking for something simpler.
Use a Canvas with fixed width and height and put it inside a Viewbox - this is a simple solution, but when resizing the Viewbox the whole content of Canvas is resized too. I want the content to have fixed size.
Is there a simple solution or should I implement my own panel (or maybe extend one of the existing ones, i.e. Canvas)?
Cheers!
I would:
subclass Canvas, perhaps calling it RelativeCanvas or RatioCanvas
add two attached properties: XRatio and YRatio
override ArrangeOverride and loop over all children. For each child, use their XRatio and YRatio along with the ActualWidth and ActualHeight of the RelativeCanvas to calculate and apply values for their Canvas.Left and Canvas.Top attached properties
You would use it as follows:
<local:RelativeCanvas>
<!-- the top-left of this button will be center of panel -->
<Button local:RelativeCanvas.XRatio="50" local:RelativeCanvas.YRatio="50"/>
</local:RelativeCanvas>
One thing you might like to add after you get that working is control over alignment. For example, I might to align the center of a control to the specified ratio, not its top-left corner.
There's one here: WPF Proportional Panel

WPF Adorner Clipping

I have an ItemsControl in a ScrollViewer. The items in the ItemsControl are expanded to a DataTemplate which basically consists of an Adorner.
Now the problem is, when scrolling, the Visual Children of the Adorner are visible outside the ScrollViewer. Lets say I scroll from the Horizontal Offset 0 to 100, the Visual Children of the Adorner move to the left and are visible next to the ScrollViewer, although they should be hidden. Setting ClipToBounds on the ItemsControl or the ScrollViewer does not work.
I understand, that Adorner are rendered above all elements z-order wise, but they really shouldn't be visible in such cases as with the ScrollViewer. The adorned Element by the way behaves like expected and is not visible through the ScrollViewer.
Is there any easy way to "clip" the Adorners, so that they are only visible in the visible scroll area?
Thanks,
Andrej
Setting ClipToBounds on the containing control is not enough. You must set the adorner's IsClipEnabled property too.
I've encountered the same problem when subclassing the WPFToolkit DataGrid to draw an adorner around the current cell.
The content of the ScrollViewer is rendered by a ScrollContentPresenter instance. ScrollContentPresenter has its own adorner layer, which is accessible through the ScrollContentPresenter.AdornerLayer property.
I found that my adorner correctly clips if I add it to that layer.
My solution was to push a clip region onto the drawing context, render whatever I needed, and pop the clipping at the end, like this:
drawingContext.PushClip(new RectangleGeometry(new Rect(0, 0, this.AdornedElement.RenderSize.Width, this.AdornedElement.RenderSize.Height)));
// continue drawing
drawingContext.Pop();
You can plug this in into any Adorner, the bounds are already available as part of the element.

Resources