wpf listbox 選中項 上移下移


private void MoveUp_Click(object sender, RoutedEventArgs e)

        {
            DataRowView rowView = this.listScrip.SelectedItem as DataRowView;
            if (rowView == null)
            {
                return;
            }


            DataRow selRow = rowView.Row;
            int index = dtScrip.Rows.IndexOf(selRow);
            if (index == 0)
            {
                return;
            }


            DataRow newRow = dtScrip.NewRow();
            newRow.ItemArray = dtScrip.Rows[index].ItemArray;             
            dtScrip.Rows.Remove(selRow);
            dtScrip.Rows.InsertAt(newRow, index - 1);


            this.listScrip.SelectedIndex = index - 1;
        }


        private void MoveDown_Click(object sender, RoutedEventArgs e)
        {
            DataRowView rowView = this.listScrip.SelectedItem as DataRowView;
            if (rowView == null)
            {
                return;
            }
            DataRow selRow = rowView.Row;
            int index = dtScrip.Rows.IndexOf(selRow);
            if (index == dtScrip.Rows.Count - 1)
            {
                return;
            }


            DataRow newRow = dtScrip.NewRow();
            newRow.ItemArray = dtScrip.Rows[index].ItemArray;
            dtScrip.Rows.Remove(selRow);
            dtScrip.Rows.InsertAt(newRow, index + 1);


            this.listScrip.SelectedIndex = index + 1;
        }
發佈了59 篇原創文章 · 獲贊 16 · 訪問量 37萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章