pallavi more

pallavi more

  • 1.6k
  • 167
  • 5k

object drop in canvas on button click event

Sep 12 2018 12:55 AM
i create grid in xaml
 
<Grid >
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<ScrollViewer x:Name="scrollviewer1"
Visibility="Visible" Grid.Row="1" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" PreviewMouseDown="scrollviewer1_PreviewMouseDown">
<Grid x:Name="MainStK" AllowDrop="True" >
</Grid>
</ScrollViewer>
</Grid>
 
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