Render an image and overlaid controls in exact pixels? - c#

I have a background image that consists of an outline, with 10 rectangles placed specifically on the outline. I want to display that image on the screen, and overlay transparent buttons that match up to the rectangles on the image. When the button is pressed, the button's outline will turn to orange to indicate the rectangle has been selected.
I'm finding that simply setting the width and height of the buttons to match the pixel width and height of the rectangles in the image always renders a button much larger than the corresponding rectangle.
The image itself is 669 x 871px, and the rectangles are all 87 x 143px. Setting the Image's WidthRequest and HeightRequest in code or in XAML seems to render it at a different resolution than it actually is, thereby leading to the issue I described - if a button's size is set to 87 x 143 (again using HeightRequest and WidthRequest) it always renders larger than the rectangle.
Furthermore, if I contain the buttons in a StackLayout (as I often need to do) and specify a margin to push the layout downwards to align with the rectangles on the image, again the correct number of pixels never matches up with the distance the layout is moved.
I'm aware of how to overlay elements using a Grid with a single row and column and simply defining multiple layouts in the same space and triggering visibility in code. What I don't know how to do is display the elements in exactly the right pixel sizes that I need for the controls to always align with the image.
The diagram I'm working with:
Desired behaviour:
Actual behaviour:

I've worked out how to do this by using Grids within a Grid. By setting the Grid's row and column height and width to "*" (use all available space), defining a ContentView containing the diagram, then several smaller grids to lay over the top with their row height and column width matching the dimensions of the diagram image (i.e. if the image is 600 pixels wide, the nested Grid's ColumnDefinition's Width="600"),
I was then able to define the buttons inside a StackLayout, inside the nested Grids. I've tried this out across a few different resolutions and it seems to work just fine for my purposes.
I had to set Padding to 0, and used the Margin to vertically align the StackLayout as needed (i.e. for the first row of buttons at the top of the diagram, their StackLayout's Margin was "19,89,19,0" - 19 pixels from either side, 89 pixels from the top).

Related

Windows phone - stretch image

I want to stretch the image on the whole page.
Fill is not appropriate - the proportions are not saved.
UniformToFill - leaves an empty space above and below.
I need to image spread over the whole page to the conservation scale.Part of the image will be clipped. How do I do this?
Both UniformToFill and Fill take the whole available space, but with different stretching behaviours. If you see empty space, that means that not the whole space is available. Maybe you placed Margins or set explicit Width and Height on Image (or placed the Image in a container with Margins or Width/Height).

Set the scrollable area size of WPF's scrollviewer

I use ScrollViewer to be able to scroll the content inside the scrollviewer. Now I also need to be able to zoom the contents, and I use RenderTransform to scale up the contents, but the ScrollViewer doesn't recognize the scaled content (the scrollable area doesn't grow).
How can I manually calculate and the set the ScrollViewer's scrollable area? Let's sat i'd like the scrollable area to be like 1000 x 1000 pixels, even when the content is only 100 x 100 pixels wide.
If you want the ScrollViewer to accommodate the scaled content, just set the content's LayoutTransform instead of its RenderTransform. The key difference is that the effects of a LayoutTransform are taken into consideration during measure and arrange, and it sounds like that's exactly what you want.

Aligning vertical or horizontal grids with percentage in WinForms

I'm working on a winforms User Control . There are several grids on it. Some of them are horizontally aligned and some of them are vertically aligned.
I'm embedding the User Control in a Form and i'm setting
this.Dock = DockStyle.Fill;
On the run, i want the grids to be scaled horizontally or vertically when fulscreen or minimized screen but i see the absolute heights and widths of grids. For example in minimized screen only the grid on the left is visible and when I make fulscreen I see the right grid.(the same for above and below grids)
How can I make them to be scaled relatively whatever the ecreen size is?
You could separate the grids with SplitContainers(Just nest as much of them as you need). Set FixedPanel to None so that the panels in the splitcontainer are sized relatively to each other. Set IsSplitterFixed to true to prohibit manual resizing. When you anchor your grids to all sides or set DockStyle to Fill you should get what you want.

Estimate position and Width/Height of rendered UserControl

I have a UserControl (boxes) that can have varying size based on the number of items in its ItemsControl.
Many such usercontrols are added to a Canvas programmatically.
I need to draw arrows interconnecting these usercontrols. What is the best way to get the origin coordinates of the control w.r.t the Canvas and the rendered Width/Height so that I can figure out the arrow start and endpoints.
Canvas provides the coordinates of each control via Canvas.Left and Canvas.Top attached properties - which you know if you positioned them yourself anyway. So the (slightly) harder part is getting the other coordinate, and for that you want to know the rendered height/width. ActualHeight and ActualWidth give you this, assuming the control has already been laid out:
double top = Canvas.GetTop(control)
double bottom = top + control.ActualHeight
double left = Canvas.GetLeft(control)
double right = left + control.ActualWidth
If you're doing this before the controls have had a chance to be rendered on the screen, you can first do control.UpdateLayout() (or control.Measure()) to ensure the layout system measures their size.

wpf: resizing a control according to its content

I have a control that inherits from Grid, it is a grid of hexagons that are generated dynamically according to the properties.
each of the hexagons is a button and a child of the Grid, and they have a style that displays them as hexagons.
what I want is for the grid to change its size according to the total size of the hexagons.
(I can calculate the exact size needed, but I don't know how to set it).
Basically you've got several options. A simple one is calculating the size yourself and assigning to the Grid's Width and Height.
A more elaborate solution would be to ask yourself a question: which layout is needed for my items? There are some standard containers which do the layout themselves and can grow/shrink with the content. For example, if your objects are just aligned in a line, you can go for StackPanel.

Categories