WPF DragEnter未在Image上触发

时间:2015-04-02 22:40:36

标签: c# wpf

我有一个标签项内的图片:

       <TabItem x:Name="tabThreeTb" Header="Photos" HorizontalAlignment="Left" Height="22" VerticalAlignment="Top" Width="55" Margin="1,0,-1,0">
            <Grid x:Name="tabThreeBdy" Background="#FFE5E5E5">
                <Rectangle Fill="#FFE5E5E5" HorizontalAlignment="Left" Height="369" Margin="12,13,0,0" Stroke="Black" VerticalAlignment="Top" Width="467">
                    <Rectangle.Effect>
                        <DropShadowEffect/>
                    </Rectangle.Effect>
                </Rectangle>
                <TextBox x:Name="picNotesTextBox" HorizontalAlignment="Left" Height="415" Margin="498,10,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="299"/>
                <Button x:Name="nxtPhotoBtn" Content="Next" HorizontalAlignment="Left" Margin="404,403,0,0" VerticalAlignment="Top" Width="75"/>
                <Button x:Name="prevPhotoBtn" Content="Prev" HorizontalAlignment="Left" Margin="10,403,0,0" VerticalAlignment="Top" Width="75"/>
                <Label x:Name="photoNumLbl" Content="1 of 4" HorizontalAlignment="Left" Margin="226,401,0,0" VerticalAlignment="Top" Width="42"/>
                <Image x:Name="photoTabImage" HorizontalAlignment="Left" Height="369" Margin="12,13,0,0" VerticalAlignment="Top" Width="467" AllowDrop="True" DragEnter="photoTabImage_DragEnter"/>
            </Grid>
        </TabItem>

我正在尝试使用拖放功能将照片添加到包含图像源路径的列表中,但我似乎无法触发DragEnter例程... < / p>

我希望拖放功能仅在内容被拖动到图像边界时才能生效。

对于嵌套在制表符控件中的Item,我需要做些什么才能允许这个?

2 个答案:

答案 0 :(得分:0)

尝试添加

    <Label HorizontalAlignment="{Binding ElementName=photoTabImage, Path=HorizontalAlignment}" 
           Height="{Binding ElementName=photoTabImage, Path=Height}"
           Width="{Binding ElementName=photoTabImage, Path=Width}"
           Margin="{Binding ElementName=photoTabImage, Path=Margin}"
           VerticalAlignment="{Binding ElementName=photoTabImage, Path=VerticalAlignment}"
           AllowDrop="True" Drop="ContainerDrop" DragOver="ContainerDragOver"/>

在Image之后,并使用事件DragOver

如果你需要分析drop对象,那么在你的类中添加下一个代码

    private void ContainerDrop(object sender, DragEventArgs e)
    {
      StringBuilder sb = new StringBuilder();
      foreach (string format in e.Data.GetFormats())
      {
        sb.AppendLine("Format:" + format);
        try
        {
          object data = e.Data.GetData(format);
          sb.AppendLine("Type:" + (data == null ? "[null]" : data.GetType().ToString()));
          sb.AppendLine("Data:" + data.ToString());
        }
        catch (Exception ex)
        {
          sb.AppendLine("!!CRASH!! " + ex.Message);
        }
        sb.AppendLine("=====================================================");
      }
      Console.WriteLine(sb.ToString());
    }
    private void ContainerDragOver(object sender, DragEventArgs e)
    {
      e.Effects = DragDropEffects.Copy;
      e.Handled = true;
    }

答案 1 :(得分:0)

问题在于,您的应用由于某种原因无法进行标准事件设置。要修复此问题,只需将预览版本上的事件替换为隧道模型(例如将DragEnter="photoTabImage_DragEnter"替换为PreviewDragEnter="photoTabImage_DragEnter"

祝你好运, MAKS!