Reputation: 566
I'm implementing a drag and drop wpf application and i have created 3 ellipse, i'm using thumb control to drag and drop the ellipse in the map, and i wish to get the drop position for ellipse. However i get an error when drag ellipse as below:
Unable to cast object of type 'System.Windows.Controls.Grid' to type 'System.Windows.Shapes.Ellipse'
My XAML:
<Window x:Class="DragandDropMFP.MainWindow"
xmlns:local="clr-namespace:DragandDropMFP"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
Title="MainWindow" Height="350" Width="525">
<Grid x:Name="maingrid" MouseMove="MainGrid_MouseMove">
<Grid.Background>
<ImageBrush ImageSource="/Map/Capture.Png" />
</Grid.Background>
<Canvas>
<Thumb Canvas.Left="38" Canvas.Top="22" DragDelta="Thumb_DragDelta">
<Thumb.Template>
<ControlTemplate>
<Viewbox Width="50" Height="50">
<Grid Width="20" Height="20">
<Ellipse
Fill="Blue"
MouseMove="DragMouseMove"/>
<TextBlock HorizontalAlignment="Center" Text="Printer1" FontSize="4" TextAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</Viewbox>
</ControlTemplate>
</Thumb.Template>
</Thumb>
<Thumb Canvas.Left="37" Canvas.Top="100" DragDelta="Thumb_DragDelta">
<Thumb.Template>
<ControlTemplate>
<Viewbox Width="50" Height="50">
<Grid Width="20" Height="20">
<Ellipse Fill="Yellow"
MouseMove="DragMouseMove"/>
<TextBlock HorizontalAlignment="Center" Text="Printer2" FontSize="4" TextAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</Viewbox>
</ControlTemplate>
</Thumb.Template>
</Thumb>
<Thumb Canvas.Left="37" Canvas.Top="174" DragDelta="Thumb_DragDelta">
<Thumb.Template>
<ControlTemplate>
<Viewbox Width="50" Height="50">
<Grid Width="20" Height="20">
<Ellipse Fill="Red"
MouseMove="DragMouseMove"/>
<TextBlock HorizontalAlignment="Center" Text="Printer3" FontSize="4" TextAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</Viewbox>
</ControlTemplate>
</Thumb.Template>
</Thumb>
</Canvas>
<TextBlock x:Name="ctlStatus" HorizontalAlignment="Stretch" Height="30" TextWrapping="Wrap" Text="status" VerticalAlignment="Bottom" RenderTransformOrigin="0.495,-4.7" />
</Grid>
</Window>
My xaml.cs :
public MainWindow()
{
InitializeComponent();
}
private void MainGrid_MouseMove(object sender, MouseEventArgs e)
{
Mouse.OverrideCursor = Cursors.Arrow;
objmoveposition(sender, e);
}
private void DragMouseMove(object sender, MouseEventArgs e)
{
Mouse.OverrideCursor = Cursors.Hand;
objmoveposition(sender, e);
}
private void objmoveposition(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
if (Mouse.OverrideCursor == Cursors.Hand)
{
Ellipse objTextbox = (Ellipse)sender; <--Error
if (objTextbox != null)
{
//----< Move Control >----
Point mousePoint = e.GetPosition(this);
//< Vertical >
int posY = (int)mousePoint.Y;
int actHeight = (int)Application.Current.MainWindow.Height;
int margin_Bottom = actHeight - (posY + (int)objTextbox.Height + (int)SystemParameters.CaptionHeight + (int)SystemParameters.BorderWidth + 4);
//< Horizontal >
int posX = (int)mousePoint.X;
int actWidth = (int)Application.Current.MainWindow.Width;
int margin_Right = actWidth - (posX + (int)objTextbox.Width + (int)SystemParameters.BorderWidth);
ctlStatus.Text = "Top=" + posY + " margin_bottom=" + margin_Bottom + " WinHeigth=" + actHeight + Environment.NewLine + " Left=" + posX + " margin_Right=" + margin_Right + "WinWidth=" + actWidth;
//ctlStatus.Text = "position=" + objTextbox.ActualHeight;
}
}
}
}
private void Thumb_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
{
UIElement thumb = e.Source as UIElement;
Canvas.SetLeft(thumb, Canvas.GetLeft(thumb) + e.HorizontalChange);
Canvas.SetTop(thumb, Canvas.GetTop(thumb) + e.VerticalChange);
}
Your guidance is highly appreciated! Thanks!
Upvotes: 2
Views: 5911
Reputation: 566
Just found out by changing the Ellipse to Grid and everything works like cham..
Ellipse objTextbox = (Ellipse)sender;
change to
Grid objTextbox = (Grid)sender;
Thanks!
Upvotes: 1
Reputation: 137128
The sender
is the ellipse that raised the event, so you can replace this:
Ellipse objTextbox = ellipse1;
with this;
Ellipse objTextbox = (Ellipse)sender;
As long as you only assign the event to ellipses this is safe. If it gets assigned to other object types you'll need to check the type of sender before casting.
You don't need the name of the object.
Upvotes: 3