2015-08-27 22 views
13

Uygulamamda, bir kullanıcı bir Image seçebilir ve onunla oynamak için bir Grid ürününe sürükleyebilir. Bunu, Grid'un PointerEntered olayını ele alarak yapıyorum. Burada kullanıcının seçilen bir görüntüye sahip olup olmadığını ve kullanıcının fare düğmesini tutup tutmadığını tespit ederim. Image kendi ManipulationStarted, ManipulationDelta ve ManipulationCompleted olayları kullanır böyleceResim ManipulationMode yakalama işaretçisini göster

Şimdi, ızgarada Image yerleştirin ve benim Image için (hala basılı) pointer geçmek istiyorum. Bu, kullanıcının öğeyi serbest bırakmak ve üzerine tıklamak yerine görüntü listesinden tek bir hareketle Grid'a sürüklemesine izin vermelidir.

ben PointerEntered içinde sender gelen işaretçi bırakmadan ve CapturePointer kullanarak yakalayan denedi, ama bu bile CapturePointer getiri true olsa çalışmak görünmüyor. İşte

Ben PointerEntered olay için kullanmak kodudur:

Benim manipülasyon kod Bu yanıt ise
private void DrawingArea_OnPointerEntered(object sender, PointerRoutedEventArgs e) 
{ 
    // If we enter the grid while dragging and we have an image that was dragged 
    if (e.Pointer.IsInContact && CurrentDraggedImage != null) 
    { 
     DrawingArea.Children.Add(CurrentDraggedImage); 

     // Move it to the location we're currently at 
     var transform = (CurrentDraggedImage.RenderTransform as CompositeTransform); 
     transform.TranslateX += e.GetCurrentPoint(DrawingArea).RawPosition.X - DrawingArea.ActualWidth/2; 
     transform.TranslateY += e.GetCurrentPoint(DrawingArea).RawPosition.Y - DrawingArea.ActualHeight/2; 

     // This works (I think) 
     (sender as UIElement).ReleasePointerCaptures(); 
     // This doesn't work (or it isn't what I need), but returns true 
     CurrentDraggedImage.CapturePointer(e.Pointer); 

     // Get ready for a new image 
     CurrentDraggedImage = null; 
    } 
} 

:

https://stackoverflow.com/a/32230733/1009013

+0

Farklı bir yaklaşım düşündünüz mü - ListView'den yerleşik CanDragItems'i kullanın ve AllowDrop'u çizim alanınızda true olarak ayarlayın. –

+0

@JustinXL Hayır, ama sanırım bu aynı yol haritasını veriyor ... Ben kontrol edip size geri döneceğim. – vrwim

+0

@JustinXL yup, aynı sorun, işaretçiyi bir sürükle-bırak eyleminden diğerine taşıyamaz ... – vrwim

cevap

0

Neden sadece sürükle n kullanmayın düşürmek? bir ızgara (sürüklemek için görüntülerin örneğin bir liste) senin araç çubuğunu içeren ve yanıt veren bir hedef ızgara oluşturma komutlarını dragdrop için:

<Grid> 

    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="Auto" /> 
     <ColumnDefinition Width="Auto" /> 
     <ColumnDefinition /> 
    </Grid.ColumnDefinitions> 

    <ListBox Background="AliceBlue" MouseMove="OnMouseMove"> 

     <ListBox.Resources> 
      <Style TargetType="{x:Type Image}"> 
       <Setter Property="Width" Value="64" /> 
       <Setter Property="Height" Value="64" /> 
      </Style> 
     </ListBox.Resources> 

     <Image Source="http://www.wpclipart.com/recreation/games/chess/chess_set_1/chess_piece_white_pawn_T.png" /> 
     <Image Source="http://www.wpclipart.com/recreation/games/chess/chess_set_1/chess_piece_white_rook_T.png" /> 
     <Image Source="http://www.wpclipart.com/recreation/games/chess/chess_set_1/chess_piece_white_knight_T.png" /> 
     <Image Source="http://www.wpclipart.com/recreation/games/chess/chess_set_1/chess_piece_white_bishop_T.png" /> 
     <Image Source="http://www.wpclipart.com/recreation/games/chess/chess_set_1/chess_piece_white_queen_T.png" /> 
     <Image Source="http://www.wpclipart.com/recreation/games/chess/chess_set_1/chess_piece_white_king_T.png" /> 
     <Image Source="http://www.wpclipart.com/recreation/games/chess/chess_set_1/chess_piece_black_pawn_T.png" /> 
     <Image Source="http://www.wpclipart.com/recreation/games/chess/chess_set_1/chess_piece_black_rook_T.png" /> 
     <Image Source="http://www.wpclipart.com/recreation/games/chess/chess_set_1/chess_piece_black_knight_T.png" /> 
     <Image Source="http://www.wpclipart.com/recreation/games/chess/chess_set_1/chess_piece_black_bishop_T.png" /> 
     <Image Source="http://www.wpclipart.com/recreation/games/chess/chess_set_1/chess_piece_black_queen_T.png" /> 
     <Image Source="http://www.wpclipart.com/recreation/games/chess/chess_set_1/chess_piece_black_king_T.png" /> 
    </ListBox> 

    <GridSplitter Grid.Column="1" Width="5" Background="LightGray" /> 

    <Grid x:Name="targetGrid" Grid.Column="2" AllowDrop="True" DragEnter="OnDragEnter" DragOver="OnDragMove" DragLeave="OnDragLeave" Drop="OnDrop" Background="Transparent"/> 

</Grid> 

Kişisel listbox bir görüntü sürüklenirken algılamak için bir MouseMove işleyicisi ihtiyacı vardır ve komut işleyicileri

public partial class MainWindow : Window 
{ 
    private Image DragImage = null; 

    public MainWindow() 
    { 
     InitializeComponent(); 
    } 

    private void OnMouseMove(object sender, MouseEventArgs e) 
    { 
     // make sure we have an image 
     var image = e.OriginalSource as Image; 
     if (image == null) 
      return; 

     // make sure we've started dragging 
     if (e.LeftButton != MouseButtonState.Pressed) 
      return; 

     DragDrop.DoDragDrop(image, image, DragDropEffects.Copy); 
    } 

    private void OnDragEnter(object sender, DragEventArgs e) 
    { 
     // make sure we have an image 
     if (!e.Data.GetDataPresent(typeof(Image)))   
     { 
      e.Effects = DragDropEffects.None; 
      return; 
     } 

     // clone the image 
     var image = e.Data.GetData(typeof(Image)) as Image; 
     e.Effects = DragDropEffects.Copy; 
     this.DragImage = new Image { Source = image.Source, Width=64, Height=64 }; 
     var position = e.GetPosition(this.targetGrid); 
     this.DragImage.SetValue(Grid.MarginProperty, new Thickness(position.X-32, position.Y-32, 0, 0)); 
     this.DragImage.SetValue(Grid.HorizontalAlignmentProperty, HorizontalAlignment.Left); 
     this.DragImage.SetValue(Grid.VerticalAlignmentProperty, VerticalAlignment.Top); 
     this.DragImage.IsHitTestVisible = false; // so we don't try and drop it on itself 

     // add it to the target grid 
     targetGrid.Children.Add(this.DragImage); 
    } 

    private void OnDragMove(object sender, DragEventArgs e) 
    { 
     var position = e.GetPosition(this.targetGrid); 
     this.DragImage.SetValue(Grid.MarginProperty, new Thickness(position.X - 32, position.Y - 32, 0, 0)); 
    } 

    private void OnDragLeave(object sender, DragEventArgs e) 
    { 
     targetGrid.Children.Remove(this.DragImage); 
     this.DragImage = null; 
    } 


    private void OnDrop(object sender, DragEventArgs e) 
    { 
     this.DragImage.IsHitTestVisible = true; 
     this.DragImage = null; 
    } 

} 

Sonuç:

enter image description here

basitçe bir resim gereklidir klonlama ve buna göre ızgaranın yüzüne sürükleyerek, gerektiği gibi çeşitli olaylara yanıt

Temiz ve zarif MVVM yerine burada korkunç ve çirkin WPF yolunu yaptım, ama fikri anladınız. Ayrıca bir Tuval yerine neden ızgarayı sürüklemek istediğinizi anlamıyorum.

+0

Şimdi bu konuya geri dönüyorum ve şimdi sürükle-bırak işini yapmaya çalışacağım Medeni bir şekilde (şu anda eklemek için dokunun). Bir Grid kullanmayı hatırlıyorum çünkü Tuvali bilmiyordum ve diğer pek çok öğe doğru seçim gibi görünmüyor ya da birden fazla çocuğa izin vermiyordu. – vrwim

+0

Şimdi Sürükle & Bırak kullanarak uyguladım, UWP 'DragDrop.DoDragDrop' gibi görünmüyor, bu yüzden' CanDragItems' kullanmak gerekiyordu. Ayrıca, ListView, kaydırmak için (uygulama bir tablette geliyor) yakaladığı için, 'OnMouseMove' yakalama biçiminiz UWP üzerinde çalışmaz. – vrwim