2012-09-11 37 views
1

我有這個XAML WPF窗口:如何刪除文件到WPF Image控件列表框裏面

<Window x:Class="WpfApplication2.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525"> 

    <Window.Resources> 
     <Style TargetType="Border" x:Key="BorderStyle"> 
      <Setter Property="BorderBrush" Value="Black"/> 
      <Setter Property="BorderThickness" Value="1"/> 
     </Style> 
     <Style TargetType="Image" x:Key="ImageStyle"> 
      <Setter Property="Height" Value="75"/> 
      <Setter Property="Width" Value="75"/> 
      <Setter Property="AllowDrop" Value="True"/> 
     </Style> 
    </Window.Resources> 
    <Grid> 
     <GroupBox> 
      <Grid> 
       <ListBox x:Name="listbox"> 
        <ListBox.ItemTemplate> 
         <DataTemplate> 
          <StackPanel Orientation="Horizontal" > 
           <TextBlock Text="{Binding}"/> 
           <Border Style="{StaticResource BorderStyle}"> 
            <Image Style="{StaticResource ImageStyle}" Drop="ThisDrop"/> 
           </Border> 
           <Border Style="{StaticResource BorderStyle}"> 
            <Image Style="{StaticResource ImageStyle}" Drop="ThatDrop"/> 
           </Border> 
          </StackPanel> 
         </DataTemplate> 
        </ListBox.ItemTemplate> 
       </ListBox> 
      </Grid> 
     </GroupBox> 
    </Grid> 
</Window> 

而這後面的代碼:

using System.Linq; 

namespace WpfApplication2 
{ 
    public partial class MainWindow 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
      listbox.ItemsSource = Enumerable.Range(1, 3); 
     } 

     private void ThisDrop(object sender, System.Windows.DragEventArgs e) 
     { 
      // do something 
     } 

     private void ThatDrop(object sender, System.Windows.DragEventArgs e) 
     { 
      // do something else 
     } 
    } 
} 

我有一個很難得到那些當我從Windows資源管理器拖放圖像控件上的文件時,將事件放下。

項目: https://github.com/ronnieoverby/WpfApplication2

+0

您是否在圖像元素上設置了AllowDrop爲true? –

+0

我已經在heirarchy中的每個元素上設置了allowdrop = true,只是試圖讓這個工作。 –

+0

DragEnter會觸發嗎? –

回答

0

試試這個:

<ListBox> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <!-- Using a StackPanel here since DataTemplate can't contain more than one element--> 
      <StackPanel Orientation="Horizontal"> 
       <Image AllowDrop="True" Drop="ImageDrop1"/> 
       <Image AllowDrop="True" Drop="ImageDrop2"/> 
      </StackPanel> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

這裏是Drop事件處理程序

private void ImageDrop1(object sender, DragEventArgs e) 
{ 
    MessageBox.Show("Dropped onto Image 1"); 
    e.Handled = true; 
} 

private void ImageDrop2(object sender, DragEventArgs e) 
{ 
    MessageBox.Show("Dropped onto Image 2"); 
    e.Handled = true; 
} 
0

這裏有一個基本的拖放操作:

private void Image_DragEnter(object sender, DragEventArgs e) 
{ 
    if (e.Data.GetDataPresent(DataFormats.FileDrop)) 
    { 
     // Signal the user they can copy files here 
     e.Effects = DragDropEffects.Copy; 
     e.Handled = true; 
    } 
} 

private void Image_Drop(object sender, DragEventArgs e) 
{ 
    string[] fileList = e.Data.GetData(DataFormats.FileDrop) as string[]; 

    if (fileList != null) 
    { 
     foreach (string file in fileList) 
     { 
      // Do stuff 
     } 

     e.Handled = true; 
    } 
} 

我通常執行的dragover並dragEnter事件簡單地調用的dragover因爲我發現有時如果DragEnter事件總是不火。但那是使用Windows窗體,可能不是WPF所必需的。

設置DragDropEffects會做兩件事。它會更改圖標,以便用戶可以看到會發生什麼(複製,移動等)。它還告訴調用者(啓動拖放的應用程序)發生了什麼,以便知道如何作出反應。例如,對於移動操作,它可能會刪除原始文件。

您可以使用e.AllowedEffects查看調用者應用程序支持的效果。例如,它可能不支持移動,只能複製。這取決於兩端的開發人員確保他們的應用程序符合Link,Move和Copy的含義。就API而言,它隻影響向用戶顯示的圖標。

1

貌似問題是,圖像控制源還沒有被設定。

0

也許你應該製作一個可以接收ICommand的AttachedProperty,然後(在視圖模型中)在這個命令中放置代碼;在附屬屬性中訂閱OnDrag事件並執行ICommand,然後在視圖(圖像)中將附加屬性綁定到該命令。

+0

我爲什麼要這麼做?聽起來好像在爲我設計,並沒有採取任何措施來解決我遇到的問題。 –

+0

對不起。我說這是因爲我之前使用過這個。如果您仍然想要,我可以提供代碼來執行此操作。 –

+0

我並不真正關心這個項目的MVVM原理,無論如何我解決了我的問題。看到我的答案。 –