Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi
can anyone please help me on how to select multiple canvas children at a time

Thank you
Maheshwar
Posted
Comments
Fredrik Bornander 30-Jun-14 9:27am    
Select in what sense?
Sergey Alexandrovich Kryukov 30-Jun-14 14:28pm    
Exactly; the problem is: "select" is not defined... OP probably needs to introduce the concept of selection first, which can make sense, say, for a vector graphics editor based on Canvas. It may involve creation of separate objects used to visualize selection (say, adorners). We need to know the exact goals of all this activity before trying to advise.
—SA
Member 10838492 1-Jul-14 0:53am    
yes
I am not able to select, when i do that the whole canvas gets selected instead of the images on canvas
Nguyen.H.H.Dang 30-Jun-14 21:25pm    
What do you mean in "select multiple canvas" ? Please provide more information.
Member 10838492 1-Jul-14 0:56am    
I have updated the code and the issue, please do have a look at it :)

1 solution

hi The problem is i have a images on the canvas and i want to select the images on click and drag... for the same purpose i have used two canvas...
1. the canvas on which images are there (.png format)
2. the canvas which is used for click and drag.

here i s the code for the same

////////////////////////////////////////////////////////////////Code///////////////////////////
XAML;


<grid x:name="theGrid" xmlns:x="#unknown">
MouseDown="Grid_MouseDown"
MouseUp="Grid_MouseUp"
MouseMove="Grid_MouseMove"
Background="Transparent"
> <!---->
<canvas name="myCanvas" horizontalalignment="Center" verticalalignment="Stretch" allowdrop="True" drop="DropImage" mouserightbuttondown="myCanvas_MouseRightButtonDown_1" width="648" margin="9,9,9,7" grid.column="1" grid.row="3">
<!---->
<image margin="2,0,0,10" name="imgPhoto" mouseleftbuttondown="DragImage" allowdrop="True" mouserightbuttondown="Image_MouseRightButtonDown_1">
Stretch="Fill" Grid.Row="1" Grid.ColumnSpan="4" Grid.Column="1" Grid.RowSpan="2" />

<canvas.contextmenu>
<contextmenu foreground="White">
<menuitem header="Delete" click="Delete1">
<menuitem header="Clear Canvas" click="CLEAR_EVENT1">
<menuitem header="new" click="new_session">
<menuitem header="copy" command="ApplicationCommands.Copy">
<menuitem header="paste" command="ApplicationCommands.Paste">

</canvas.contextmenu>
<!---->


<!---->
<canvas>
<!-- This canvas is overlaid over the previous canvas and is used to
place the rectangle that implements the drag selection box. -->
<rectangle>
x:Name="selectionBox"
Visibility="Collapsed"
Stroke="Black"
StrokeThickness="1"
/>
</canvas>
<!---->


XAML.cs

bool mouseDown = false; // Set to 'true' when mouse is held down.
Point mouseDownPos; // The point where the mouse button was clicked down.

private void Grid_MouseDown(object sender, MouseButtonEventArgs e)
{
// Capture and track the mouse.
mouseDown = true;
mouseDownPos = e.GetPosition(theGrid);
theGrid.CaptureMouse();

// Initial placement of the drag selection box.
Canvas.SetLeft(selectionBox, mouseDownPos.X);
Canvas.SetTop(selectionBox, mouseDownPos.Y);
selectionBox.Width = 0;
selectionBox.Height = 0;

// Make the drag selection box visible.
selectionBox.Visibility = Visibility.Visible;
myCanvas.Children.Contains(selectedElement);
}

private void Grid_MouseUp(object sender, MouseButtonEventArgs e)
{
// Release the mouse capture and stop tracking it.
mouseDown = false;
theGrid.ReleaseMouseCapture();

// Hide the drag selection box.
selectionBox.Visibility = Visibility.Collapsed;

Point mouseUpPos = e.GetPosition(theGrid);


}

private void Grid_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
{
if (mouseDown)
{

// When the mouse is held down, reposition the drag selection box.

Point mousePos = e.GetPosition(theGrid);

if (mouseDownPos.X < mousePos.X)
{
Canvas.SetLeft(selectionBox, mouseDownPos.X);
selectionBox.Width = mousePos.X - mouseDownPos.X;
}
else
{
Canvas.SetLeft(selectionBox, mousePos.X);
selectionBox.Width = mouseDownPos.X - mousePos.X;
}

if (mouseDownPos.Y < mousePos.Y)
{
Canvas.SetTop(selectionBox, mouseDownPos.Y);
selectionBox.Height = mousePos.Y - mouseDownPos.Y;
}
else
{
Canvas.SetTop(selectionBox, mousePos.Y);
selectionBox.Height = mouseDownPos.Y - mousePos.Y;
}

myCanvas.Children.Contains(selectedElement);

if (selected)
{
selected = false;
if (selectedElement != null)
{
// Remove the adorner from the selected element

//<<i have justifed it after adding the delete button becasue of adorner layer issue>>

aLayer.Remove(aLayer.GetAdorners(selectedElement)[0]);
selectedElement = null;
}
}


if (e.Source != myCanvas)
{
_isDown = true;
_startPoint = e.GetPosition(myCanvas);

selectedElement = e.Source as UIElement;

_originalLeft = Canvas.GetLeft(selectedElement);
_originalTop = Canvas.GetTop(selectedElement);

aLayer = AdornerLayer.GetAdornerLayer(selectedElement);

aLayer.Add(new ResizingAdorner(selectedElement));
selected = true;
e.Handled = true;

}
}
}
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 1-Jul-14 1:28am    
How can you consider this as an answer and even accept it formally?
—SA
Member 10838492 2-Jul-14 3:24am    
hey Hi
its not the solution yaar, its just a part of my code, I have by mistake posted here
Sergey Alexandrovich Kryukov 2-Jul-14 10:55am    
No problem; just use comments and/or "Improve question" next time. Note that if you comment on someone's post, the author of that post receives notification (if not opted out).
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900