To draw a rectangle on a WPF canvas, you can create an instance of the Rectangle class and set its properties, then add it to the canvas using the Children property.
Here's an example:
<Canvas Name="myCanvas" Width="400" Height="400"/>
In this example, we define a Canvas element with a Name of myCanvas and a Width and Height of 400.
using System.Windows.Media; using System.Windows.Shapes; public class Program { static void Main() { // Create a rectangle Rectangle rectangle = new Rectangle { Width = 100, Height = 50, Fill = Brushes.Blue, Stroke = Brushes.Black, StrokeThickness = 2 }; // Add the rectangle to the canvas myCanvas.Children.Add(rectangle); // Position the rectangle Canvas.SetLeft(rectangle, 50); Canvas.SetTop(rectangle, 50); } } In this example, we create a Rectangle object and set its properties using object initializer syntax. We set the Width and Height properties to 100 and 50, respectively, and set the Fill property to Brushes.Blue, the Stroke property to Brushes.Black, and the StrokeThickness property to 2.
We then add the Rectangle object to the Children property of the Canvas object using the Add method.
Finally, we use the Canvas.SetLeft and Canvas.SetTop methods to position the rectangle on the canvas.
When you run this code, it will draw a blue rectangle with a black border on the canvas, positioned at (50, 50).
"Draw a basic rectangle on WPF canvas"
<Canvas Name="myCanvas" Background="White"> <Rectangle Width="100" Height="50" Fill="Blue" Canvas.Left="50" Canvas.Top="50"/> </Canvas>
"WPF canvas rectangle with border"
<Canvas Name="myCanvas" Background="White"> <Rectangle Width="100" Height="50" Fill="Blue" Stroke="Black" StrokeThickness="2" Canvas.Left="50" Canvas.Top="50"/> </Canvas>
"Draw a rounded rectangle on WPF canvas"
<Canvas Name="myCanvas" Background="White"> <Rectangle Width="100" Height="50" Fill="Blue" Stroke="Black" StrokeThickness="2" RadiusX="10" RadiusY="10" Canvas.Left="50" Canvas.Top="50"/> </Canvas>
"WPF canvas rectangle with mouse interaction"
<Canvas Name="myCanvas" Background="White" MouseLeftButtonDown="Canvas_MouseLeftButtonDown"> <Rectangle Width="100" Height="50" Fill="Blue" Stroke="Black" StrokeThickness="2" Canvas.Left="50" Canvas.Top="50"/> </Canvas>
private void Canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { // Handle mouse click on the rectangle } "WPF canvas rectangle with resizing handles"
<Canvas Name="myCanvas" Background="White" MouseLeftButtonDown="Canvas_MouseLeftButtonDown"> <Rectangle x:Name="myRectangle" Width="100" Height="50" Fill="Blue" Stroke="Black" StrokeThickness="2" Canvas.Left="50" Canvas.Top="50"/> </Canvas>
private void Canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { // Implement resizing handles logic (e.g., using adorners) } "WPF canvas rectangle animation"
<Canvas Name="myCanvas" Background="White"> <Rectangle x:Name="myRectangle" Width="100" Height="50" Fill="Blue" Stroke="Black" StrokeThickness="2" Canvas.Left="50" Canvas.Top="50"> <Rectangle.Triggers> <EventTrigger RoutedEvent="Loaded"> <BeginStoryboard> <Storyboard> <DoubleAnimation Storyboard.TargetProperty="Width" To="200" Duration="0:0:2"/> <DoubleAnimation Storyboard.TargetProperty="Height" To="100" Duration="0:0:2"/> </Storyboard> </BeginStoryboard> </EventTrigger> </Rectangle.Triggers> </Rectangle> </Canvas>
"WPF canvas rectangle with drag and drop"
<Canvas Name="myCanvas" Background="White" MouseLeftButtonDown="Rectangle_MouseLeftButtonDown" MouseMove="Rectangle_MouseMove" MouseLeftButtonUp="Rectangle_MouseLeftButtonUp"> <Rectangle x:Name="myRectangle" Width="100" Height="50" Fill="Blue" Stroke="Black" StrokeThickness="2" Canvas.Left="50" Canvas.Top="50"/> </Canvas>
private bool isDragging = false; private Point offset; private void Rectangle_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { isDragging = true; offset = e.GetPosition(myRectangle); } private void Rectangle_MouseMove(object sender, MouseEventArgs e) { if (isDragging) { Point mousePos = e.GetPosition(myCanvas); Canvas.SetLeft(myRectangle, mousePos.X - offset.X); Canvas.SetTop(myRectangle, mousePos.Y - offset.Y); } } private void Rectangle_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { isDragging = false; } "WPF canvas rectangle with context menu"
<Canvas Name="myCanvas" Background="White"> <Rectangle Width="100" Height="50" Fill="Blue" Stroke="Black" StrokeThickness="2" Canvas.Left="50" Canvas.Top="50"> <Rectangle.ContextMenu> <ContextMenu> <MenuItem Header="Delete" Click="DeleteMenuItem_Click"/> </ContextMenu> </Rectangle.ContextMenu> </Rectangle> </Canvas>
private void DeleteMenuItem_Click(object sender, RoutedEventArgs e) { myCanvas.Children.Remove(myRectangle); } "WPF canvas rectangle with dynamic creation"
<Canvas Name="myCanvas" Background="White" MouseLeftButtonDown="Canvas_MouseLeftButtonDown"> <!-- No initial rectangle in XAML --> </Canvas>
private void Canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { Rectangle newRectangle = new Rectangle { Width = 100, Height = 50, Fill = Brushes.Green, Stroke = Brushes.Black, StrokeThickness = 2 }; Canvas.SetLeft(newRectangle, e.GetPosition(myCanvas).X); Canvas.SetTop(newRectangle, e.GetPosition(myCanvas).Y); myCanvas.Children.Add(newRectangle); } uicollectionview pg-dump css-gradients matplotlib matlab-table azure-aks appium bufferedreader tar react-hooks