2010-10-23 167 views
2

我創建了一個包含文件名的列表框。我想給用戶一個選擇,使用向上/向下按鈕和使用拖放來上下文件名。在WPF中向上/向下移動ListBoxItem

任何人都有一個想法如何實現這一功能。

XAML代碼:

<ListBox 
    Grid.Column="0" 
    Name="listBox1" 
    AllowDrop="True" 
    Drop="listBox1_Drop" 
/> 
<StackPanel 
    Grid.Column="1" 
    HorizontalAlignment="Stretch" 
    VerticalAlignment="Center"> 
    <Button 
     Name="moveUp" 
     Content="Ç" 
     FontFamily="Wingdings 3" 
     Margin="3,3,3,3" 
     Click="moveUp_Click" /> 
    <Button 
     Name="moveDown" 
     FontFamily="Wingdings 3" 
     Content="È" 
     Margin="3,3,3,3" /> 
</StackPanel> 

回答

7

如果你不想要實現的東西比複雜的移和Move Down可以像這樣處理。如果源看起來像這樣

public ObservableCollection<FileClass> FileNames 
{ 
    get; 
    set; 
} 

private void moveUp_Click(object sender, RoutedEventArgs e) 
{ 
    FileClass selectedfile = listBox1.SelectedItem as FileClass; 
    int index = FileNames.IndexOf(selectedfile); 
    if (index > 0) 
    { 
     FileNames.Remove(selectedfile); 
     FileNames.Insert(index-1, selectedfile); 
     listBox1.SelectedItem = selectedfile; 
    } 
} 

private void moveDown_Click(object sender, RoutedEventArgs e) 
{ 
    FileClass selectedfile = listBox1.SelectedItem as FileClass; 
    int index = FileNames.IndexOf(selectedfile); 
    if (index < FileNames.Count-1) 
    { 
     FileNames.Remove(selectedfile); 
     FileNames.Insert(index + 1, selectedfile); 
     listBox1.SelectedItem = selectedfile; 
    } 
} 

CHANGE
試試這個代碼與上下拖動移動項目和列表框

private void listBox1_Drop(object sender, DragEventArgs e) 
{ 
    ListBox parent = sender as ListBox; 
    FileClass data = e.Data.GetData(typeof(FileClass)) as FileClass; 
    FileClass objectToPlaceBefore = GetObjectDataFromPoint(parent, e.GetPosition(parent)) as FileClass; 
    if (data != null && objectToPlaceBefore != null) 
    { 
     int index = FileNames.IndexOf(objectToPlaceBefore); 
     FileNames.Remove(data); 
     FileNames.Insert(index, data); 
     listBox1.SelectedItem = data; 
    } 
} 

private void listBox1_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
{ 
    ListBox parent = sender as ListBox; 
    FileClass data = GetObjectDataFromPoint(parent, e.GetPosition(parent)) as FileClass; 
    if (data != null) 
    { 
     DragDrop.DoDragDrop(parent, data, DragDropEffects.Move); 
    } 
} 

private static object GetObjectDataFromPoint(ListBox source, Point point) 
{ 
    UIElement element = source.InputHitTest(point) as UIElement; 
    if (element != null) 
    { 
     object data = DependencyProperty.UnsetValue; 
     while (data == DependencyProperty.UnsetValue) 
     { 
      data = source.ItemContainerGenerator.ItemFromContainer(element); 
      if (data == DependencyProperty.UnsetValue) 
       element = VisualTreeHelper.GetParent(element) as UIElement; 
      if (element == source) 
       return null; 
     } 
     if (data != DependencyProperty.UnsetValue) 
      return data; 
    } 

    return null; 
} 

這應該完成拖放內下降。

+0

謝謝您的回答。它工作完美。對於拖放操作,我仍然在工作,因爲它在同一個ListBox上,而按鈕用於向上和向下,用戶可以使用拖放來排列列表。 – asifabbas 2010-10-23 18:46:15

+0

好吧,我誤解了,認爲你想從另一個控制中拖下去。 – 2010-10-23 18:56:36

+0

已更新我的示例以通過拖放來重新排列列表 – 2010-10-23 20:28:24

0

如果綁定你的按鈕,你可以試試這個:

private void MoveItemUp() 
    { 
     if (SelectedGroupField != null) 
     { 
      List<string> tempList = AvailableGroupField; 
      string selectedItem = SelectedGroupField; 

      int currentIndex = tempList.IndexOf(selectedItem); 

      if (currentIndex > 0) 
      { 
       tempList.Remove(selectedItem); 
       tempList.Insert(currentIndex - 1, selectedItem); 
       AvailableGroupField = null; 
       AvailableGroupField = tempList; 
       SelectedGroupField = AvailableGroupField.Single(p => p == selectedItem); 
      } 
     } 
    } 

    private void MoveItemDown() 
    { 
     if (SelectedGroupField != null) 
     { 
      List<string> tempList = AvailableGroupField; 
      string selectedItem = SelectedGroupField; 

      int currentIndex = tempList.IndexOf(selectedItem); 

      if (currentIndex < (tempList.Count - 1)) 
      { 
       tempList.Remove(selectedItem); 
       tempList.Insert(currentIndex + 1, selectedItem); 
       AvailableGroupField = null; 
       AvailableGroupField = tempList; 
       SelectedGroupField = AvailableGroupField.Single(p => p == selectedItem); 
      } 
     }   
    }