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);
Related
How can i resize Polygon which is placed in Canvas so it fills that canvas?
I don't want to change values of polygon points, I just want it to be displayed inside of canvas in case polygon coordinates are too large, too small or out of screen.
Came across this problem recently and worked out a solid solution:
<Canvas x:Name="CanvasName">
<Polygon Points="{Binding Path=PointCollectionName}" Stretch="Fill" Fill="Black"
Width={Binding ElementName=CanvasName, Path=ActualWidth}"
Height={Binding ElementName=CanvasName, Path=AcutalHeight}"/>
</Canvas>
Do you have the chance to replace the Canvas with a Grid? If so just set the Stretch attribute of the polygon, e.g. Uniform keeps the aspect ratio:
<Grid Width="297" Height="159">
<Polygon Points="10,110 110,110 110,10" Fill="Blue" Stretch="Uniform" />
</Grid>
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.
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.
in the following example, my border wraps around the image. The border does not wrap tightly around the image because I use DecodePixelWidth to keep the aspect ratio. Two sides end up with the border right up against the image, and the other two have gaps from the control. Is there a clean way to have the border wrap the image while keeping the aspect ratio instead of setting the Image stretch to fill.
BitmapImage bitmapIkon = new BitmapImage();
bitmapIkon.BeginInit();
bitmapIkon.CacheOption = BitmapCacheOption.OnLoad;
bitmapIkon.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
bitmapIkon.UriSource = new Uri(imagePath);
bitmapIkon.DecodePixelWidth = decodePixelWidth;
bitmapIkon.EndInit();
iImage.MinWidth=width;
iImage.MinHeight=height;
iImage.Source = bitmapIkon;
<Border Width="Auto" Height="Auto" Name="borderImageData" HorizontalAlignment="Center" VerticalAlignment="Center" BorderBrush="Black" BorderThickness="1" CornerRadius="0">
<Image Name="iImage" Stretch="Uniform" />
</Border>
Something like this should work:
<Grid HorizontalAlignment="Center" VerticalAlignment="Center">
<TextBox Name="iImage" Text="Uniform" Margin="1" />
<Border Name="borderImageData" BorderBrush="Black" BorderThickness="1" CornerRadius="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
</Grid>
So effectively, the Grid sizes to fit the Image plus a margin of 1. The Border then stretches to fill the Grid, and draws it's border on top of the image.
If you are going to be using this a lot, then you may want to wrap it in a custom control.
I have an image that the user can zoom/scroll.
I want to draw some rectangles/circles on a different layer (for example: drawing a circle for each person's face that was identified in the picture).
The rectangle position is relative to the image.
How do I create such an overlay?
An easy way is to just use a Canvas and set the canvas' background property to your photo, and then place your circles or rectangles on top of that and position them with the Canvas.Left and .Top properties.
<Canvas x:Name="myCanvas">
<Canvas.Background>
<ImageBrush ImageSource="c:\photo.bmp"/>
</Canvas.Background>
<Image Canvas.Top="20" Canvas.Left="20" Height="20" Width="20" Source="c:\circle.bmp"/>
</Canvas>
I have managed to do something similar:
Set image as background
Put a transparent ItemsControl on top of it
Set ItemsControl.ItemsPanel to Canvas
wrote handlers for dragging operations
Code Snippet:
<ItemsControl x:Name="overlayItemsControl"
Background="Transparent"
ItemsSource="{Binding Path=Blocks}"
Width="{Binding ElementName=imageControl, Path=Width}"
Height="{Binding ElementName=imageControl, Path=Height}"
ItemContainerStyle="{StaticResource rectStyle}"
PreviewMouseMove="ItemsControl_PreviewMouseMove"
PreviewMouseDown="ItemsControl_PreviewMouseDown"
PreviewMouseUp="ItemsControl_PreviewMouseUp"
PreviewKeyDown="ItemsControl_PreviewKeyDown">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas IsItemsHost="True" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
....
</ItemsControl>