Tell me what the error is. I want to use drag & drop to insert a picture, but after dragging, I get an Image object with the Source property set to null

XAML

  <Grid.ColumnDefinitions> <ColumnDefinition Width="300"></ColumnDefinition> <ColumnDefinition Width="*"></ColumnDefinition> </Grid.ColumnDefinitions> <ScrollViewer Grid.RowSpan="2"> <StackPanel CanVerticallyScroll="True"> <Image Source="Resources/part1.jpg" Width="280" Height="121" Margin="0,10,0,10" MouseDown="Image_MouseDown" Stretch="UniformToFill"/> <Image Source="Resources/part2.jpg" Width="280" Height="121" Margin="0,10,0,10" MouseDown="Image_MouseDown" Stretch="UniformToFill"/> <Image Source="Resources/part3.jpg" Width="280" Height="121" Margin="0,10,0,10" MouseDown="Image_MouseDown" Stretch="UniformToFill"/> <Image Source="Resources/part4.jpg" Width="280" Height="121" Margin="0,10,0,10" MouseDown="Image_MouseDown" Stretch="UniformToFill"/> <Image Source="Resources/part5.jpg" Width="280" Height="121" Margin="0,10,0,10" MouseDown="Image_MouseDown" Stretch="UniformToFill"/> <Image Source="Resources/part6.jpg" Width="280" Height="121" Margin="0,10,0,10" MouseDown="Image_MouseDown" Stretch="UniformToFill"/> </StackPanel> </ScrollViewer> <Canvas Grid.Column="1" Margin="0,0,0,0" > <Image Source="Resources/part1.jpg" Width="280" Height="120" Drop="Image_Drop" DragEnter="Image_DragEnter" AllowDrop="True" Stretch="UniformToFill" Canvas.Left="156" Canvas.Top="51"/> <Image Source="Resources/part2.jpg" Width="280" Height="120" Drop="Image_Drop" AllowDrop="True" Stretch="UniformToFill" Canvas.Left="436" Canvas.Top="51"/> <Image Source="Resources/part3.jpg" Width="280" Height="120" Drop="Image_Drop" AllowDrop="True" Stretch="UniformToFill" Canvas.Left="156" Canvas.Top="170"/> <Image Source="Resources/part4.jpg" Width="280" Height="120" Drop="Image_Drop" AllowDrop="True" Stretch="UniformToFill" Canvas.Left="436" Canvas.Top="170"/> <Image Source="Resources/part5.jpg" Width="280" Height="120" Drop="Image_Drop" AllowDrop="True" Stretch="UniformToFill" Canvas.Left="156" Canvas.Top="288"/> <Image Source="Resources/part6.jpg" Width="280" Height="120" Drop="Image_Drop" AllowDrop="True" Stretch="UniformToFill" Canvas.Left="436" Canvas.Top="288"/> </Canvas> </Grid> 

C #

 public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void Image_Drop(object sender, DragEventArgs e) { ((Image) sender).Source = e.Source as ImageSource; } private void Image_MouseDown(object sender, MouseButtonEventArgs e) { // sender – ΠΎΠ±ΡŠΠ΅ΠΊΡ‚, Π½Π° ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠΌ ΠΏΡ€ΠΎΠΈΠ·ΠΎΡˆΠ»ΠΎ Π΄Π°Π½Π½ΠΎΠ΅ событиС. Image lbl = sender as Image; // Π‘ΠΎΠ·Π΄Π°Π΅ΠΌ источник. // ΠšΠΎΠΏΠΈΡ€ΡƒΠ΅ΠΌ содСрТимоС ΠΌΠ΅Ρ‚ΠΊΠΈ Drop. // 1 ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€: Π­Π»Π΅ΠΌΠ΅Π½Ρ‚ управлСния, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ Π±ΡƒΠ΄Π΅Ρ‚ источником. // 2 ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€: Π”Π°Π½Π½Ρ‹Π΅, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ Π±ΡƒΠ΄ΡƒΡ‚ ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Ρ‰Π°Ρ‚ΡŒΡΡ. // 3 ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€: Π­Ρ„Ρ„Π΅ΠΊΡ‚ ΠΏΡ€ΠΈ пСрСносС. DragDrop.DoDragDrop(lbl, lbl.Source, DragDropEffects.Copy); } private void Image_DragEnter(object sender, DragEventArgs e) { e.Effects = DragDropEffects.Copy; } } 

link to git https://github.com/Sechik/Puzzle

    1 answer 1

    You have different Source (you are trying to assign them):

     {pack://application:,,,/Puzzle;component/Resources/part3.jpg} {System.Windows.Controls.Image} 

    This is how it is copied, but the picture is the same in sender and e

     private void Image_Drop(object sender, DragEventArgs e) { //((Image) sender).Source = e.Source as ImageSource; Image image = e.Source as Image; ((Image)sender).Source = image.Source; } 

    Here is a ready-made solution (here the picture is passed through an additional variable):

     using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace Puzzle { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public static Image global_sender; public MainWindow() { InitializeComponent(); } private void Image_Drop(object sender, DragEventArgs e) { //((Image) sender).Source = e.Source as ImageSource; //Image image = e.Source as Image; //((Image)sender).Source = image.Source; ((Image)sender).Source = global_sender.Source; } private void Image_MouseDown(object sender, MouseButtonEventArgs e) { // sender – ΠΎΠ±ΡŠΠ΅ΠΊΡ‚, Π½Π° ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠΌ ΠΏΡ€ΠΎΠΈΠ·ΠΎΡˆΠ»ΠΎ Π΄Π°Π½Π½ΠΎΠ΅ событиС. Image lbl = sender as Image; global_sender = lbl; // Π‘ΠΎΠ·Π΄Π°Π΅ΠΌ источник. // ΠšΠΎΠΏΠΈΡ€ΡƒΠ΅ΠΌ содСрТимоС ΠΌΠ΅Ρ‚ΠΊΠΈ Drop. // 1 ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€: Π­Π»Π΅ΠΌΠ΅Π½Ρ‚ управлСния, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ Π±ΡƒΠ΄Π΅Ρ‚ источником. // 2 ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€: Π”Π°Π½Π½Ρ‹Π΅, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ Π±ΡƒΠ΄ΡƒΡ‚ ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Ρ‰Π°Ρ‚ΡŒΡΡ. // 3 ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€: Π­Ρ„Ρ„Π΅ΠΊΡ‚ ΠΏΡ€ΠΈ пСрСносС. DragDrop.DoDragDrop(lbl, lbl.Source, DragDropEffects.Copy); } private void Image_DragEnter(object sender, DragEventArgs e) { e.Effects = DragDropEffects.Copy; } } } 

    enter image description here

    • Thank. Here are just a couple of questions: 1) What does Source mean different? 2) Image image = e.Source as Image; ((Image) sender) .Source = image.Source; why this code? - Alexandr
    • In the F5 debugging mode, put a breakpoint on these lines, hover the cursor and open the pop-up menu - there will be their contents. - codename0082016
    • Extra code. take away - codename0082016