无法将类型 'System.Windows.Controls.Grid' 的对象转换为类型 'System.Windows.Shapes.Ellipse'?

Unable to cast object of type 'System.Windows.Controls.Grid' to type 'System.Windows.Shapes.Ellipse'?

我正在实现一个拖放 wpf 应用程序,我已经创建了 3 个椭圆,我正在使用拇指控件在地图中拖放椭圆,我希望获得椭圆的放置位置。但是,如下所示拖动椭圆时出现错误:

Unable to cast object of type 'System.Windows.Controls.Grid' to type 'System.Windows.Shapes.Ellipse'

我的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>

我的 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);
        }

输出:

非常感谢您的指导! 谢谢!

sender 是引发事件的椭圆,因此您可以替换为:

Ellipse objTextbox = ellipse1;

有了这个;

Ellipse objTextbox = (Ellipse)sender;

只要您只将事件分配给省略号,这是安全的。如果它被分配给其他对象类型,您需要在投射之前检查发件人的类型。

您不需要对象的名称。

刚刚通过将 Ellipse 更改为 Grid 发现一切都像 cham..

Ellipse objTextbox = (Ellipse)sender; 

改为

Grid objTextbox = (Grid)sender;

谢谢!