2012-07-23 43 views
0

我試圖找到一種方法來拖放列表框項目和國際象棋棋盤之間使用WPF &。我有一個左側的列表框和右側的棋盤。我怎樣才能拖動一個項目,然後拖入棋盤的一個或多個方格。然後點擊廣場,會顯示有關這裏的項目的一些信息。我很感激,如果有人能幫助我?謝謝大家。在列表框項目和國際象棋棋盤之間拖放

回答

0

這裏是我的高清完成..但我在這裏拖動文件從桌面到我的列表框

` public MainPage() 
    { 
     InitializeComponent(); 

     CompositionTarget.Rendering +=new EventHandler(CompositionTarget_Rendering); 

     FileBoard.Drop += new DragEventHandler(FileBoard_Drop); 
    } 

`

當拖動元素

void FileBoard_Drop(object sender, DragEventArgs e) 
    { 

     if (e.Data != null) 
     { 
      FileInfo[] files = e.Data.GetData(DataFormats.FileDrop) as FileInfo[]; 



      foreach (FileInfo fi in files) 
      { 
       _files.Enqueue(fi); 
      } 

     } 


    } 

創建使用CompositionTargetRendering u能雙端隊列文件

private void CompositionTarget_Rendering(Object sender, EventArgs e) 
    { 
     if (_files.Count != 0) 
     { 
      // Create a photo 
      FileInfo fi = _files.Dequeue(); 

     } 
    } 

,然後的ItemSource分配給烏爾板或下棋列表DATAinGrid

盒子項目...嘗試修改代碼我想你會得到它

0

這是舊的,但我發現了一個KISS解決方案。

使用TextBlocks或網格中的圖像創建您的棋盤網格。

爲要傳遞的數據創建一個模型。

在XML

做到這一點:

<TextBlock x:Name="myname" x:Uid="myname" Grid.Row="0" Grid.Column="1" Margin="3" Text="{Binding myfield}" Style="{DynamicResource myStyle}" AllowDrop="True" Drop="Square_Drop"/> 
<TextBlock x:Name="myname" x:Uid="myname" Grid.Row="1" Grid.Column="1" Margin="3" Text="{Binding myfield}" Style="{DynamicResource myStyle}" AllowDrop="True" Drop="Square_Drop"/> 
//etc etc etc 

<ListBox x:Name="myListBox" x:Uid="myListBox" 
        ItemContainerStyle="{DynamicResource myListBoxListItemStyle}" Margin="10" 
        DisplayMemberPath="myField" 
        PreviewMouseLeftButtonDown="List_PreviewMouseLeftButtonDown"> 

我會強烈建議您使用資源,將TextBlock /圖像正方形的造型。 (!一個白,一個黑)

Learn how here

然後在C#後面你將需要:

private void List_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
    { 
     if (myListBox.SelectedItem != null) 
     { 
      ListBox parent = (ListBox)sender; 
      myModel data = parent.SelectedItem as myModel; 

      if (data != null) 
      { 
       DragDrop.DoDragDrop(parent, data, DragDropEffects.Move); 
      } 
     } 
    } 

    private void Square_Drop(object sender, DragEventArgs e) 
    { 
     MyModel data = e.Data.GetData(typeof(MyModel)) as MyModel; 

     TextBlock tb = sender as TextBlock; 

     tb.DataContext = data; 

     //Add any database update code here 

     refreshInterface(); 
    }