How to resize a rectangle from the bottom up? XAML - c#

The figure below shows me changing two values ​​for height. When I decrease the height instead of the figure decreasing from top to bottom the opposite happens.
<Rectangle Fill="#DBDBDB" HorizontalAlignment="Left" Height="100" Margin="547,607,0,0" Stroke="Silver" StrokeThickness="2" VerticalAlignment="Top" Width="100" RenderTransformOrigin="0,0.97" />
I just need to know how to set X and Y

If your rectangle happens to be on a Canvas, you can make use of the Canvas.Right and Canvas.Bottom
<Canvas>
<Button Click="Button_Click">Shrink It a Bit</Button>
<Rectangle x:Name="_Rectangle" Canvas.Bottom="20" Canvas.Right="20"
Fill="Black" Height="50" Width="50" />
</Canvas>
So now if you shrink it:
_Rectangle.Height = _Rectangle.Height - 5;
_Rectangle.Width = _Rectangle.Width - 5;
you'll notice that it shrinks from the top and left. You can use the appropriate combinations of Top/Bottom, Right/Left to get the effect you desire.

Related

Set padding of a Canvas in WPF

I have a Canvas with a Rectangle and a Circle inside it:
<Canvas x:Name="CanvasMain" Width="595" Height="842" Background="White" HorizontalAlignment="Center" >
<Rectangle Fill="Tomato" Height="335" Canvas.Left="40" Stroke="Black" Canvas.Top="60" Width="265"/>
<Ellipse Fill="Tomato" Height="175" Canvas.Left="370" Stroke="Black" Canvas.Top="465" Width="200"/>
</Canvas>
I want to set Padding of the Canvas programmatically. Should I set the margin of all the elements inside the Canvas to achieve this purpose or is there any alternative
I want to set Padding of the Canvas programmatically. Should I set the margin of all the elements inside the Canvas to achieve this purpose or is there any alternative
It is either that or adjusting the coordinates (the Canvas.Top and Canvas.Left properties) of the elements.
A Canvas has no concept of padding so you need to create the gap yourself somehow. There is no right or wrong really.
I think you can try to put the canvas in a border.
<Border x:Name="rootBorder">
<Canvas x:Name="CanvasMain" Width="595" Height="842" Background="White" HorizontalAlignment="Center" >
</Canvas>
</Border>
Then you can set border's padding.
rootBorder.Padding = new Thickness(25);

Zoom and move image with code

I have an image inside a border and I would like to show different parts of the image source in the image box at different times. Specifically, when a certain textbox gets focus, I want to change the image so it zooms to a certain portion of the image content.
Here's the XAML:
<Border BorderBrush="Silver" BorderThickness="1" Grid.Column="5" Height="504"
HorizontalAlignment="Left" Margin="20,116,0,0" Name="border1"
VerticalAlignment="Top" Width="410" ClipToBounds="True">
<Image Height="493" Name="image5" Stretch="Fill" Width="390"
ClipToBounds="True" BindingGroup="{Binding}"
Clip="{Binding ElementName=border1}"
Cursor="Hand" StretchDirection="Both" />
</Border>
For an example, my image source is 2550 x 3320 pixels. I would like the image box to show the source in a rectangle starting at point 1755,300 with width of 650 and height of 230. I do not want to use CloneBitmap to cut that rectangle out and display it because I also have a manual zoom set up for this image where the user can use the mouse wheel to zoom in and out and click & drag to pan the image. I still want to allow that after we zoom to desired area.
UPDATE:
I've tried implementing colinsmith's answer, but whenever I change the scrollviewer's offsets, It chops the image, so if I later move (click and drag to pan) it, it's empty space. I've had this working before with just the image inside the scrollviewer, but now I have an image inside a scrollviewer inside a border. The border is necessary for my zoom and pan as I have it set up now.
My updated XAML:
<Border BorderBrush="Silver" BorderThickness="1" Grid.Column="5" Height="504"
HorizontalAlignment="Left" Margin="20,116,0,0" Name="border1"
VerticalAlignment="Top" Width="410" ClipToBounds="True">
<ScrollViewer x:Name="image5scroll" VerticalScrollBarVisibility="Hidden"
HorizontalScrollBarVisibility="Hidden">
<Image Height="493" Name="image5" Stretch="Fill" Width="390"
ClipToBounds="True" BindingGroup="{Binding}"
Clip="{Binding ElementName=image5scroll}" Cursor="Hand"
StretchDirection="Both" />
</ScrollViewer>
</Border>
And code behind:
public void imageZoom(Element element, int index)
{
if (element.Rectangle.Left - 100 > 0)
{
image5scroll.ScrollToHorizontalOffset(element.Rectangle.Left - 100);
image5scroll.Width = element.Rectangle.Width + 200;
image5scroll.Height = element.Rectangle.Height + 200;
border1.Width = image5scroll.Width;
border1.Height = image5scroll.Height;
image5.Width = image5scroll.Width;
image5.Height = image5scroll.Height;
image5.Stretch = System.Windows.Media.Stretch.None;
}
else
{
image5scroll.ScrollToHorizontalOffset(0);
}
if (element.Rectangle.Top - 100 > 0)
{
image5scroll.ScrollToVerticalOffset(element.Rectangle.Top - 100);
}
else
{
image5scroll.ScrollToVerticalOffset(0);
}
}
You can use a ScrollViewer to wrap your Image...then you can tell the ScrollViewer to go to a vertical and horizontal offset by calling the ScrollToVerticalOffset() and ScrollToHorizontalOffset() methods (as there isn't a property which you can set).
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<Border BorderBrush="Silver" BorderThickness="1" Grid.Column="5" Height="504"
HorizontalAlignment="Left" Margin="20,116,0,0" Name="border1" VerticalAlignment="Top" Width="410">
<ScrollViewer x:Name="image5scroll" VerticalScrollBarVisibility="Hidden" HorizontalScrollBarVisibility="Hidden">
<Image Height="493" Name="image5" Stretch="None" Width="390" BindingGroup="{Binding}"
Cursor="Hand" Source="http://www.noaanews.noaa.gov/stories/images/goes-12-firstimage-large081701.jpg"/>
</ScrollViewer>
</Border>
</Grid>
</Page>
However, if you must be be able to set the position of the ScrollViewer using properties instead of code-behind then a solution is to define an Attached Property which does the calls to ScrollToVerticalOffset/ScrollToHorizontalOffset for you underneath.
http://marlongrech.wordpress.com/2009/09/14/how-to-set-wpf-scrollviewer-verticaloffset-and-horizontal-offset/

cannot draw shape in canvas

I have a canvas in xaml defined as following. However adding a rectangle in it doesn't show any thing.
<lib:DrawingCanvas x:Name="drawingCanvas" Background="White" AllowDrop="True">
<Rectangle Margin="20,20,20,20" Fill="Black" Stroke="White" Width="100" Height="100">
</Rectangle>
</lib:DrawingCanvas>
Can you give me some hints here?
Thanks.
You're not using a Canvas, you're using a "DrawingCanvas", so I can't speak for any differences. But assuming it derives from Canvas:
You need to set the attached properties for the Rectangle. These are Canvas.Left or Canvas.Right, in addition to Canvas.Top or Canvas.Bottom.
For example:
<Rectangle Canvas.Left="50" Canvas.Top="100" Width="100" Height="100" Fill="Black" />

Silverlight - Creating a rectangle that grows/shrinks to accommodate the text of a label within it

Pretty straight forward. I have a rectangle with a label over top of it. I'f like to know how to get the rectangle to scale to fit the text.
my XAML:
<Grid x:Name="LayoutRoot" Background="White" Height="158" Width="264">
<Rectangle Height="22" HorizontalAlignment="Left" Name="rectangle1" Stroke="Black" MinWidth="40" StrokeThickness="1" VerticalAlignment="Top" RadiusX="6" RadiusY="6" Fill="#1b6487" Width="64"></Rectangle>
<sdk:Label Margin="9,3,209,0" Name="label1" VerticalAlignment="Top" Content="$999.99" />
</Grid>
Remove your explicit widths and heights.
You've got the Rectangle and the Label in the same cell of the Grid, so by default they will be the same size. You're overriding that and telling them not to be.
Alternatively, you could wrap a Border around your Label. This is the sort of thing that Border is meant for.

How to set Canvas.ZIndex to paint a black panel between the controls?

With the following code I can demonstrate how a black panel with a opacity of 50% is on top of every rectangle:
<Grid>
<Rectangle Fill="Black" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Opacity="0.5" Canvas.ZIndex="1"/>
<Rectangle Fill="Red" Width="200" Height="200" Canvas.ZIndex="0"/>
<Grid>
<Rectangle Fill="Blue" Width="100" Height="100" Canvas.ZIndex="0"/>
<Rectangle Fill="Yellow" Width="50" Height="50" Canvas.ZIndex="1"/>
</Grid>
</Grid>
It looks like this:
I would like to have the yellow rectangle above the black panel, but that seems to be impossible.
I can achieve something close by setting the ZIndex of the Grid containing both the Blue and Yellow rectangles to "1". But this would also raise the blue rectangle above the black, and this is a problem.
<Grid>
<Rectangle Fill="Black" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Opacity="0.5" Canvas.ZIndex="1"/>
<Rectangle Fill="Red" Width="200" Height="200" Canvas.ZIndex="0"/>
<Grid Canvas.ZIndex="1">
<Rectangle Fill="Blue" Width="100" Height="100" Canvas.ZIndex="0"/>
<Rectangle Fill="Yellow" Width="50" Height="50" Canvas.ZIndex="1"/>
</Grid>
</Grid>
How do I get only the yellow rectangle above the black?
In my real application I have user controls instead of the rectangles. I like to make a particular control standing out by having everything else covered by the half-black shade.
Many Thanks,
I don't think you'll be able to achieve this with your current arrangement of controls.
There are two levels of controls here, the "Blue" and "Yellow" controls inside the inner grid and then the "Black" and "Red controls together with the inner grid.
The ZIndex works on controls at the same "level" - so you can ensure that the yellow control is on top of the blue, but then at the higher level these are grouped under the inner grid so are treated as a single unit.
The only way this would work is if all your controls were at the same level. If you included a second semi opaque rectangle in the inner grid you could get the yellow to be on top of that but that might end up making other controls too dark.
One approach might be to not use just a simple black rectangle.
Instead use a Path composed of two rectangles. The first rectangle will cover the whole area and the second would just cover the control to be available.
This creates a large rectangle with a hole in it where your target control can show through and accept input.
The down side is working out the rectangle geometry to add to create the hole but that's fairly straight forward.

Categories

Resources