How to position a user control in the canvas? - silverlight

I have certain rectangular usercontrols. How can I place it at a certain x-y coordinate in the canvas? Once I place a user control, I need to evaluate the position to place the next control based on the rendered width/height of the previously placed control. How to do these correctly in Silverlight.

To position controls inside a canvas panel, you use the Canvas class's attached properties, Canvas.Left and Canvas.Top. So, if you wanted your control to be positioned at 100,20 (relative to the top-left corner of the canvas), you would enter:
<Canvas>
<Rectangle Canvas.Left="100" Canvas.Top="20"/>
</Canvas>
Alternatively, you could use this in the code-behind:
yourControl.SetValue(Canvas.LeftProperty,100.0);
yourControl.SetValue(Canvas.TopProperty,20.0);
If you've got several controls you're trying to arrange dynamically though, you may want to consider using a StackPanel or Grid intead of a Canvas.

Related

Maintain position for child in a Canvas with zoom and scroll - WPF

I would like to implement the following functionality.
I have a canvas with several UIElements inside it. The Canvas allows to zoom and scroll applying a scale and translate transforms.
I would like to maintain the red square always in the same position (left bottom corner of the Canvas), to behave as a floating control, so as I change the zoom or the scroll, the red square always maintains it's size and position. Something similar to google maps "Earth window":
What is the best approach to implement it?
NOTE: I tried to use the WPF adorner layer but it does not respond to mouse events, and I need to interact with the red square.
As Clemens said, put it in another layer on top. Grid can host multiple items in the same cell, so create a 1 by 1 Grid for your Canvas add content on top just like any other WPF layout. The later items appear on top (unless Z layer is specified):
<Grid>
<Canvas>
... do all my fancy drawing
</Canvas>
<Rectangle VerticalAlignment="Bottom" HorizontalAlignment="Left" Width="100" Height="100" />
</Grid>
Note, you may have trouble if the canvas has any non WPF rendering, such as video or embedded WindowsForms content. I've seen people have trouble with drawing WPF stuff on top of that.
I also believe you can set Canvas.Bottom="20" to set the position relative to the bottom edge, but I've never used it.

Scrollviewer & Canvas

I am trying to load an image within a canvas such that, if the size of image overflows the canvas, the scroll bars should get activated (MS Paint style)
<Window>
<ScrollViewer>
<Canvas ScrollViewer.HorizontalScrollBarVisibility="Visible"
ScrollViewer.VerticalScrollBarVisibility="Visible">
<Image Source="SampleImage.jpg" />
</Canvas>
</ScrollViewer>
</Window>
Now as Canvas is stretched to Window's size, scroll-bars won't appear as Canvas is not actually overflowing out of the Window.
Secondly, as the Image is much bigger than the Canvas, it is getting clipped at the bounds of Canvas, so ScrollViewer doesn't think that its content: Canvas is actually overflowing.
It happens a lot of time with StackPanels too, that even though the bound data has tens of rows, but still the scrollbars don't get activated. Sometimes scrollviewers appear as mystery to me.
So, what should be the basic logic kept in mind when using ScrollViewer control.
Thank you.
Edit: Just edited the question title, so that whosoever has problem with canvas can get this question easily in search.
From MSDN:
Canvas is the only panel element that has no inherent layout characteristics. A Canvas has default Height and Width properties of zero, unless it is the child of an element that automatically sizes its child elements. Child elements of a Canvas are never resized, they are just positioned at their designated coordinates. This provides flexibility for situations in which inherent sizing constraints or alignment are not needed or wanted. For cases in which you want child content to be automatically resized and aligned, it is usually best to use a Grid element.
Hovever, you can set Canvas Height and Width explicitely:
<ScrollViewer Height="100" Width="200">
<Canvas Height="400" Width="400">
//Content here
</Canvas>
</ScrollViewer>
Maybe one of these two Links might help you:
Silverlight Canvas on Scrollviewer not triggering
ScrollBars are not visible after changing positions of controls inside a Canvas

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.

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