🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: alt.pl.comp.lang.csharp
Article #22482

Drag and drop w WPF

#22482
From: Paweł
Date: Mon, 12 Aug 2013 12:49
109 lines
5235 bytes
Czesc

Mam problem z prawidłowym użyciem mechanizmu Drag and Drop w swoim 
programie. Otóż w kodzie xaml mam:

  <ListView Grid.Column="0" Grid.Row="1" Height="200" Margin="2" 
Name="lvListaWczytanychPlikowFotek" ScrollViewer.CanContentScroll="True" 
ScrollViewer.VerticalScrollBarVisibility="Auto">
                             <ListView.ItemTemplate>
                                 <DataTemplate>
                                     <StackPanel Orientation="Vertical">
                                         <Border BorderBrush="Bisque" 
BorderThickness="2" CornerRadius="5" Margin="5">
                                             <Image Margin="5" 
Source="{Binding .Foto}" Height="170" Width="150" 
MouseLeftButtonDown="DragImage" />
                                         </Border>
                                         <StackPanel>
                                             <TextBlock 
Name="tbNazwaPliku" Text="{Binding .NazwaPliku}" />
                                         </StackPanel>
                                     </StackPanel>
                                 </DataTemplate>
                             </ListView.ItemTemplate>
                         </ListView>

[...]

<StackPanel Grid.Column="1" Grid.Row="0" Grid.RowSpan="2" Margin="5" 
Background="Transparent">
	<TextBlock>Fotografia 1:</TextBlock>
         	<Border BorderThickness="2" Margin="2" BorderBrush="Beige">
                 	<Image MinWidth="200" MinHeight="200" AllowDrop="True" 
Drop="DropImageFoto" Name="image1foto" Stretch="UniformToFill"></Image>
	</Border>
</StackPanel>

W kodzie zaś, mam klasę:

public class Fotografia
         {
             public string NazwaPliku { get; set; }
             public string SciezkaDoPliku { get; set; }
             public ImageSource Foto { get; set; }

             public Fotografia()
             {

             }

             public Fotografia(string _nazwaPliku, string 
_sciezkaDoPliku, ImageSource _foto)
             {
                 this.NazwaPliku = _nazwaPliku;
                 this.SciezkaDoPliku = _sciezkaDoPliku;
                 this.Foto = _foto;
             }
         }
i obiekt:

public ObservableCollection<Fotografia> listaWczytaneFotki = new 
ObservableCollection<Fotografia>();

do którego są pobierane pliki

  listaWczytaneFotki.Add(new Fotografia{NazwaPliku = 
System.IO.Path.GetFileName(file), SciezkaDoPliku = 
System.IO.Path.GetFullPath(file),Foto = (new 
ImageSourceConverter()).ConvertFromString(file) as ImageSource });

Teraz chcę móc przeciągać obrazek z kontrolki ListView 
(lvListaWczytanychPlikowFotek) na kontrolkę Image(image1foto), tak aby 
on się w niej wyświetlił:

private void DragImage(object sender, MouseButtonEventArgs e)
         {
             //Image image = e.Source as Image;
             //DataObject data = new DataObject(typeof(ImageSource), 
image.Source);
             //DragDrop.DoDragDrop(image, data, DragDropEffects.Copy);
             //e.Handled = true;

             Fotografia current = 
(Fotografia)lvListaWczytanychPlikowFotek.Items.CurrentItem;
             ImageSource data = current.Foto;
             DragDrop.DoDragDrop(lvListaWczytanychPlikowFotek, data, 
DragDropEffects.Copy);
             e.Handled = true;
         }

  private void DropImageFoto(object sender, DragEventArgs e)
         {

             Fotografia brFoto = e.Data.GetData(typeof(Fotografia)) as 
Fotografia;
             Image imageControl = new Image() { Width = 
image1foto.Width, Height = image1foto.Height, Source = brFoto.Foto };
             imageControl.Stretch = Stretch.UniformToFill;
             this.image1foto.Source = imageControl.Source;
             e.Handled = true;

         }

Na chwilę obecną obrazki w ListView się ładnie wyświetlają, w momencie 
rozpoczęcia przeciągania wybranej fotografii kursor myszy zmienia 
kształt ale nie wykonuje się fragment kodu związany z metodą 
DropImageFoto. Co pominęłem że ten kod nie działa prawidłowo ?

Z góry dzięki za podpowiedź. Pozdrawiam
Technical Details
Message-ID: <5208d97a$0$1220$65785112@news.neostrada.pl>
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.icm.edu.pl!news.nask.pl!news.nask.org.pl!goblin1!goblin.stu.neva.ru!news.astraweb.com!border5.a.newsrouter.astraweb.com!xlned.com!feeder7.xlned.com!feeder1.cambriumusenet.nl!82.197.223.103.MISMATCH!feeder3.cambriumusenet.nl!feed.tweaknews.nl!209.197.12.246.MISMATCH!nx02.iad01.newshosting.com!newshosting.com!newsfeed.neostrada.pl!unt-exc-02.news.neostrada.pl!unt-spo-a-02.news.neostrada.pl!news.neostrada.pl.POSTED!not-for-mail

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.icm.edu.pl!news.nask.pl!news.nask.org.pl!goblin1!goblin.stu.neva.ru!news.astraweb.com!border5.a.newsrouter.astraweb.com!xlned.com!feeder7.xlned.com!feeder1.cambriumusenet.nl!82.197.223.103.MISMATCH!feeder3.cambriumusenet.nl!feed.tweaknews.nl!209.197.12.246.MISMATCH!nx02.iad01.newshosting.com!newshosting.com!newsfeed.neostrada.pl!unt-exc-02.news.neostrada.pl!unt-spo-a-02.news.neostrada.pl!news.neostrada.pl.POSTED!not-for-mail
Date: Mon, 12 Aug 2013 14:49:42 +0200
From: Paweł <gamacdur@op.pl>
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.15) Gecko/20110303 Thunderbird/3.1.9
MIME-Version: 1.0
Newsgroups: alt.pl.comp.lang.csharp
Subject: Drag and drop w WPF
Lines: 109
Message-ID: <5208d97a$0$1220$65785112@news.neostrada.pl>
Organization: Telekomunikacja Polska
NNTP-Posting-Host: 46.170.157.70
X-Trace: 1376311674 unt-rea-b-01.news.neostrada.pl 1220 46.170.157.70:55857
X-Complaints-To: abuse@news.neostrada.pl
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit