i create grid in xaml
Visibility="Visible" Grid.Row="1" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" PreviewMouseDown="scrollviewer1_PreviewMouseDown">
code behind xaml is
Button CreateButton(string rungId)
{
Button button = new Button() { VerticalAlignment = VerticalAlignment.Stretch, HorizontalAlignment = HorizontalAlignment.Stretch, Content = rungId, Tag = rungId };
button.Click += Button_Click;
return button;
}
Border CreateDesignerCanvas(string rungName)
{
Border border = new Border();
border.BorderBrush = Brushes.Black;
border.BorderThickness = new Thickness(.5);
DesignerCanvas designerCanvas = new DesignerCanvas()
{
Focusable = true,
ContextMenu = (ContextMenu)FindResource("DesignerCanvasContextMenu"),
Background = Brushes.LightYellow,
Name = rungName,
//Margin = new Thickness(3)
};
border.Child = designerCanvas;
return border;
}
void CreateGridSplitter(int rowId)
{
GridSplitter splitter = new GridSplitter()
{
VerticalAlignment = VerticalAlignment.Bottom,
HorizontalAlignment = HorizontalAlignment.Stretch,
//ResizeBehavior = GridResizeBehavior.PreviousAndNext,
Background = new SolidColorBrush(Colors.Green),
Height = 5
};
MainStK.Children.Add(splitter);
Grid.SetRow(splitter, rowId);
Grid.SetColumnSpan(splitter, 2);
}
public Window1()
{
InitializeComponent();
MainStK.RowDefinitions.Add(new RowDefinition());
MainStK.RowDefinitions.Add(new RowDefinition());
MainStK.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto });
MainStK.ColumnDefinitions.Add(new ColumnDefinition());
string rungName = string.Empty, rungName2 = string.Empty;
rungName = "Rung1";
rungName2 = "Rung2";
Border designerCanvas = CreateDesignerCanvas(rungName);
Border designerCanvas2 = CreateDesignerCanvas(rungName2);
Button button = CreateButton("1");
Button button2 = CreateButton("2");
Grid.SetRow(designerCanvas, 0);
Grid.SetRow(designerCanvas2, 1);
Grid.SetColumn(designerCanvas, 1);
Grid.SetColumn(designerCanvas2, 1);
Grid.SetColumn(button, 0);
Grid.SetColumn(button2, 0);
Grid.SetRow(button, 0);
Grid.SetRow(button2, 1);
MainStK.Children.Add(designerCanvas);
MainStK.Children.Add(designerCanvas2);
MainStK.Children.Add(button);
MainStK.Children.Add(button2);
//CreateGridSplitter(0);
//CreateGridSplitter(1);
}
now i want to place rectangle on grid using button click event what should i do
1 Reply
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Sandhiya PriyaPosted Oct 29, 2025, 10:21 AM
you’ve already got a dynamic grid setup with buttons and
DesignerCanvascontrols.Now you want to add a Rectangle shape onto the corresponding canvas when a button is clicked.
Let’s go step-by-step to make this clear and working.
Goal
When you click Button 1, a Rectangle should appear on DesignerCanvas “Rung1”.
When you click Button 2, a Rectangle should appear on DesignerCanvas “Rung2”.
Step 1: Modify
Button_ClickEventYou already wire up:
Now implement it like this
Step 2: Create Helper Method to Add Rectangle
If your
DesignerCanvasis a subclass ofCanvas(which is typical in WPF drag-drop designers),this will work perfectly — the rectangle will appear inside that canvas.
Step 3: Confirm
DesignerCanvasInherits fromCanvasMake sure your
DesignerCanvasis declared like:If it’s derived from something else (like Grid or Border),
just replace the call to
Canvas.SetLeft()andCanvas.SetTop()with layout logic suitable for that panel.
Full Working Snippet (Simplified)
Here’s a clean version for clarity:
Optional Enhancement
You can make rectangle placement dynamic — for example:
Random positions
Positions based on previous shapes count
Allow dragging (if your
DesignerCanvassupports it)Example: