WPF Grid not keeping its Width and Height - c#

I have a Canvas inside of a Grid in a WPF app. I want to draw a skeleton in Canvas from Kinect data. But the Canvas has a tiny width, probably 1px. The Grid makes it shrink, not keeping its set Width and Height. How to make Grid keep its declared size?

Make sure both the grid and the canvas have HorizontalAlignment="Stretch" and VerticalAlignment="Stretch"

Related

Horizontal percentage stretching of children

What component in MAUI would I use to accomplish such a chart?
It should stretch to 100% width and the elements should have relative widths.
Is this possible with XAML only?
I thought about a grid with one row, but I can't iterate over the children.
Or a horizontal stackpanel, but it won't stretch.
Any idea?

How do I get the width and height of a Canvas element that is set to 'Auto'?

I'm developing an App for Windows Phone 8.1, and I have a canvas that draws a ball onto it programmatically. My problem is I want to set the boundaries of the canvas to be that of its width and height, but I'm using grids with relative sizes for the size of the canvas itself.
I'm wondering how I can get the width and height of the canvas - I've tried actualwidth and actualheight but they're just showing '0'.
You can put the Canvas inside something (a Border, a Grid), and then get the ActualWidth and ActualHeight from the container.
I have the impression that Canvas does not expand to fill container, so it is better to get dimensions from the container itself. Just be sure the container layout is the same you would want your Canvas to have.
I got the width and height by implementing the SizeChanged event for the Canvas in which I call my own redraw() function. MyCanvas.ActualWidth then have the correct value.

Use a border in xaml without shrinking the contents of the border

I am using a border object in xaml with a large border thickness. However need a way to prevent the contents of the border from shrinking when i increase the border thickness, I just want them to draw over it if they are near enough to the edge. Any ideas?
Don't place your content inside the border, place it "above" it instead:
<Grid>
<Border BorderThickness="20"/>
<OtherContent/>
</Grid>

ScrollViewer's ScrollToHorizontalOffset not working in Windows 8 app

I have a Windows Store app with a ScrollViewer and an Image in it. When i double tap on the ScrollViewer I want it to zoom the Image to its width. This part is not a problem but I also want the Image to be centered after it has beed zoomed in.
I tried calling the ScrollToHorizontalOffset method on the ScrollViewer but It does not seem to work with any number I give it. What is the problem?
Perhaps the offset only works for the non-zoomed view where your image fills the ScrollViewer completely and thus can't be scrolled. You could try setting the image dimensions so that it is larger than the ScrollViewer, but set original ZoomFactor, so that it fills the ScrollViewer at first. Then zooming and scrolling might work.
Assign a SizeChangedEvent in the scrollviewer.
<ScrollViewer SizeChanged="OnSizeChange"></ScrollViewer>
like this. Then it is better to place your image inside a canvas. So your code will be probably like this.
<ScrollViewer SizeChanged="OnSizeChange" x:Name="scrl">
<Canvas RenderTransformOrigin="0.5,0.5" x:Name="main">
<Image source="" Canvas.Top="insert desire double value here", Canvas.Left="Same goes here"/>
</Canvas>
</ScrollViewer>
then in the code behind you can change the height and width of your canvass depending on the scroll viewer
Main.Width = scrl.ViewPortWidth;
Main.Height = scrl.ViewPortHeight;
You can experiment on the size of the canvass while adding a double tap event to it. Changing the size of the canvas can zoom in or out the image because the image is inside the canvass
Try with 'ChangeView' instead of 'ZoomToFactor'

How to resize a Canvas in WPF?

I'm writing a WPF app that has a Canvas in it. This canvas will be custom rendered at runtime. It's sort of like a game in that it needs to be measured in pixels. I need to be able to set my Canvas to 478x478 pixels (client rectangle size). I don't want any scaling or other resolution-independent steps to take place on my Canvas.
I'm not sure if this is possible in WPF, since its nature is to be resolution independent. My questions:
How do I resize my Canvas at runtime (function to call?)
When I resize my Canvas, is the renderable area (the client rectangle) going to be that size? If not, how can I resize it to make sure the client rectangle is a specific width/height?
Is it possible to set the width/height of the Canvas in Pixels? How does the resolution-independent aspect of WPF interfere with what I'm trying to do?
When I resize my Canvas, will other controls resize appropriately as they have been designed to do in the WPF designer?
Thanks in advance.
Any elements positioned in a canvas will not resize or reposition based upon the size of the canvas. So I don't think there's any advantage to setting the size of the canvas. Maybe you should just set a fixed size of the window instead.
Otherwise, just set the Canvas.Width, Height, and ClipToBounds=True and you have a fixed sized canvas that positions its child elements with X/Y coordinates.
Also you should be sure to set SnapsToDevicePixels=True on the canvas so that child elements will have crisp pixel-aligned bounds.
resizing is cake:
MyCanvas.Width = 350;
MyCanvas.Height = 450;
this sets the size, you CAN render to coordinates outside of this, but it will be clipped. You can wrap your canvas inside a scroller to allow the user to see what is outside the height/width of the canvas.
as for the rest of your questions, i think you can see this SO question for your answers
Update 2020: on WPF .NET CORE: If the Canvas is a child of a Viewbox, the content is automatically scaled to the Viewbox height and width.
<Viewbox Stretch="Fill"
Width="50"
Height="50">
<Canvas>
<Path.... />
</Canvas>
</Viewbox>

Categories