簡體   English   中英

無法將'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個橢圓,我使用拇指控制將橢圓拖放到地圖中,我希望得到橢圓的放置位置。 但是我拖動橢圓時會出現錯誤,如下所示:

無法將“System.Windows.Controls.Grid”類型的對象強制轉換為“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;

只要您只將事件分配給省略號,這是安全的。 如果將其分配給其他對象類型,則需要在轉換之前檢查發送方的類型。

您不需要對象的名稱。

剛剛通過將橢圓更改為網格而發現,一切都像cham ..

Ellipse objTextbox = (Ellipse)sender; 

改成

Grid objTextbox = (Grid)sender;

謝謝!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM