我有一个列表框,里面有一堆值.我还有一个UP按钮和一个DOWN按钮.使用这些按钮,我想向上/向下移动列表框中的所选项目.我这样做有困难.
到目前为止,这是我的代码:
private void btnDataUp_Click(object sender, RoutedEventArgs e)
{
int selectedIndex = listBoxDatasetValues.SelectedIndex; //get the selected item in the data list
if (selectedIndex != -1 && selectedIndex != 0) //if the selected item is selected and not at the top of the list
{
//swap items here
listBoxDatasetValues.SelectedIndex = selectedIndex - 1; //keep the item selected
}
}
Run Code Online (Sandbox Code Playgroud)
我不知道如何交换价值观!任何帮助将不胜感激!
Pet*_*sen 24
由于您已使用ItemsSource通过绑定到ObservableCollection来填充列表框,因此您无法修改列表框的Items属性.
ItemsSource只能在Items集合为空时设置,并且只有在ItemsSource为null时才能修改Items.
否则,您将收到错误"当ItemsSource正在使用时操作无效..."
你需要做的是修改底层集合,因为它是一个ObservableCollection,ListBox将反映这些变化.
以下代码显示了如何通过交换集合中的项目来上下移动项目.
相应的XAML只包含一个名为lbItems的列表框和两个用于连接事件处理程序的按钮.
public partial class MainWindow : Window
{
private ObservableCollection<string> ListItems = new ObservableCollection<string>
{
"Item 1", "Item 2", "Item 3", "Item 4", "Item 5", "Item 6"
};
public MainWindow()
{
InitializeComponent();
lbItems.ItemsSource = this.ListItems;
}
private void up_click(object sender, RoutedEventArgs e)
{
var selectedIndex = this.lbItems.SelectedIndex;
if (selectedIndex > 0)
{
var itemToMoveUp = this.ListItems[selectedIndex];
this.ListItems.RemoveAt(selectedIndex);
this.ListItems.Insert(selectedIndex - 1, itemToMoveUp);
this.lbItems.SelectedIndex = selectedIndex - 1;
}
}
private void down_click(object sender, RoutedEventArgs e)
{
var selectedIndex = this.lbItems.SelectedIndex;
if (selectedIndex + 1 < this.ListItems.Count)
{
var itemToMoveDown = this.ListItems[selectedIndex];
this.ListItems.RemoveAt(selectedIndex);
this.ListItems.Insert(selectedIndex + 1, itemToMoveDown);
this.lbItems.SelectedIndex = selectedIndex + 1;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我为此做了一些扩展方法:
public static void MoveItemUp<T>(this ObservableCollection<T> baseCollection, int selectedIndex)
{
//# Check if move is possible
if (selectedIndex <= 0)
return;
//# Move-Item
baseCollection.Move(selectedIndex - 1, selectedIndex);
}
public static void MoveItemDown<T>(this ObservableCollection<T> baseCollection, int selectedIndex)
{
//# Check if move is possible
if (selectedIndex < 0 || selectedIndex + 1 >= baseCollection.Count)
return;
//# Move-Item
baseCollection.Move(selectedIndex + 1, selectedIndex);
}
public static void MoveItemDown<T>(this ObservableCollection<T> baseCollection, T selectedItem)
{
//# MoveDown based on Item
baseCollection.MoveItemDown(baseCollection.IndexOf(selectedItem));
}
public static void MoveItemUp<T>(this ObservableCollection<T> baseCollection, T selectedItem)
{
//# MoveUp based on Item
baseCollection.MoveItemUp(baseCollection.IndexOf(selectedItem));
}
Run Code Online (Sandbox Code Playgroud)
没有必要知道ListBox.
小智 5
这是执行此操作的最简单方法,它会触发所有正确的事件,因此您不必担心 XAML。ObservableCollection 有一个很好的方法叫做
MoveItem(previousIndex, newIndex)
Run Code Online (Sandbox Code Playgroud)
鉴于您有一个名为 DataItemList 的 ObservableCollection
public void MoveUp()
{
var currentIndex = DataItemList.SelectedIndex;
//Index of the selected item
if (currentIndex > 0)
{
int upIndex = currentIndex - 1;
//move the items
DataItemList.MoveItem(upIndex,currentIndex);
}
}
Run Code Online (Sandbox Code Playgroud)
对于 Down,您将获得前一项的索引。
就那么简单!