ListBox操作(绑定、添加、删除、上移、下移、添加全部、删除……)

   //绑定ListBox1
    private void ListBox1Bind()
    {
        .....
        DataSet ds = new DataSet();
        ListBox1.DataSource = ds.Tables[0].DefaultView; //ds 为 DataSet 对象
        ListBox1.DataTextField = "textFld"; //textFld 为字段名称
        ListBox1.DataValueField = "valueFld"; //valueFld 为字段名称
        ListBox1.DataBind();
    }   

    //添加
    protected void Addbtn_Click(object sender, EventArgs e)
    {
        int i = 0;
        while (i < ListBox1.Items.Count)
        {
            if (ListBox1.Items[i].Selected == true)
            {
                ListBox2.Items.Add(ListBox1.Items[i]);
                ListBox1.Items.Remove(ListBox1.Items[i]);
            }
            else
                i += 1;
        }
    }

    //删除
    protected void Deletebtn_Click(object sender, EventArgs e)
    {
        int i = 0;
        while (i < ListBox2.Items.Count)
        {
            if (ListBox2.Items[i].Selected == true)
            {
                ListBox1.Items.Add(ListBox2.Items[i]);
                ListBox2.Items.Remove(ListBox2.Items[i]);
            }
            else
                i += 1;
        }
    }

    //上移
    protected void Upbtn_Click(object sender, EventArgs e)
    {
        //若不是第一行则上移
        if (ListBox2.SelectedIndex > 0)
        {
            string name = ListBox2.SelectedItem.Text;
            string ID = ListBox2.SelectedItem.Value;
            int index = ListBox2.SelectedIndex;
            ListBox2.SelectedItem.Text = ListBox2.Items[index - 1].Text;
            ListBox2.SelectedItem.Value = ListBox2.Items[index - 1].Value;
            ListBox2.Items[index - 1].Text = name;
            ListBox2.Items[index - 1].Value = ID;
            ListBox2.SelectedIndex--;
        }
    }

    //下移
    protected void Downbtn_Click(object sender, EventArgs e)
    {
        //若不是最后一行则下移
        if (ListBox2.SelectedIndex >= 0 && ListBox2.SelectedIndex < ListBox2.Items.Count - 1)
        {
            string name = ListBox2.SelectedItem.Text;
            string ID = ListBox2.SelectedItem.Value;
            int index = ListBox2.SelectedIndex;
            ListBox2.SelectedItem.Text = ListBox2.Items[index + 1].Text;
            ListBox2.SelectedItem.Value = ListBox2.Items[index + 1].Value;
            ListBox2.Items[index + 1].Text = name;
            ListBox2.Items[index + 1].Value = ID;
            ListBox2.SelectedIndex++;
        }
    }
  //添加全部、删除清空
  protected void Button5_Click(object sender, EventArgs e)
        {
            int i = 0;
            while (i < ListBox2.Items.Count)
            {
               ListBox1.Items.Add(ListBox2.Items[i]);
               i++;
            }
            ListBox2.Items.Clear();
        }

 

转地址:http://hi.baidu.com/lzg1018/blog/item/6c0d4acd335d3f560eb345b7.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 WPF MVVM ,可以通过使用 `ICommand` 和 `RelayCommand` 来实现 `ListBox` 上移下移操作。 以下是一个实现上移下移功能的示例代码: 1. 在视图模型,创建一个 `ObservableCollection` 来存储列表项,并创建两个 `RelayCommand` 分别用于上移下移操作: ``` public class MyViewModel : INotifyPropertyChanged { private ObservableCollection<string> _items = new ObservableCollection<string>(); public ObservableCollection<string> Items { get { return _items; } set { _items = value; OnPropertyChanged(nameof(Items)); } } public ICommand MoveUpCommand { get; set; } public ICommand MoveDownCommand { get; set; } public MyViewModel() { MoveUpCommand = new RelayCommand<object>(MoveUp, CanMoveUp); MoveDownCommand = new RelayCommand<object>(MoveDown, CanMoveDown); } private bool CanMoveUp(object parameter) { var index = Items.IndexOf(parameter as string); return index > 0; } private void MoveUp(object parameter) { var index = Items.IndexOf(parameter as string); var temp = Items[index]; Items[index] = Items[index - 1]; Items[index - 1] = temp; } private bool CanMoveDown(object parameter) { var index = Items.IndexOf(parameter as string); return index >= 0 && index < Items.Count - 1; } private void MoveDown(object parameter) { var index = Items.IndexOf(parameter as string); var temp = Items[index]; Items[index] = Items[index + 1]; Items[index + 1] = temp; } public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } } ``` 2. 在视图,使用 `ListBox` 来显示列表项,并绑定上移下移命令: ``` <ListBox ItemsSource="{Binding Items}"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <Button Content="▲" Command="{Binding DataContext.MoveUpCommand, RelativeSource={RelativeSource AncestorType={x:Type ListBox}}}" CommandParameter="{Binding}" /> <Button Content="▼" Command="{Binding DataContext.MoveDownCommand, RelativeSource={RelativeSource AncestorType={x:Type ListBox}}}" CommandParameter="{Binding}" /> <TextBlock Text="{Binding}" /> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> ``` 在上面的示例,使用了 `RelayCommand` 类来实现命令的绑定。它是一个实现了 `ICommand` 接口的类,可以用于在视图模型声明和实现命令。在 `ListBox` 的数据模板,使用了 `RelativeSource` 来绑定 `ListBox` 的父级元素(即视图),以便使用视图模型的命令。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值