如何将多个选定项目从一个列表框添加/移动到另一个列表框?

时间:2011-01-20 01:36:07

标签: c# wpf wpf-controls

我有一个小型原型,允许将所选项目从一个列表框添加到另一个列表框。我需要能够从列表框中选择多个项目并将它们移动到另一个项目并向后移动 - 从第二个列表框到第一个列表框。我想知道是否有人有好样本或可以修改我已有的代码。提前谢谢。

两个列表框和按钮XAML:

<Grid x:Name="LayoutRoot">
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>    
    <Border Margin="0,40,225,20">
        <ListBox x:Name="LeftListBox" SelectionMode="Multiple"/>
    </Border>
    <Border Margin="0,40,20,20" BorderThickness="1" BorderBrush="#FFCECECE" HorizontalAlignment="Right" Width="169" Padding="5" >
        <ListBox x:Name="RightListBox" BorderThickness="0" />
    </Border>
    <Button x:Name="AddButton" Height="40" Margin="0,3,198,0" VerticalAlignment="Center" Click="AddButton_Click" HorizontalAlignment="Right" Width="15" Content="▶" />
    <Button x:Name="RemoveButton" Height="40" Margin="0,94,198,0" VerticalAlignment="Center" Click="RemoveButton_Click" HorizontalAlignment="Right" Width="15" Content="R" />
</Grid>

代码背后:

public partial class SelectServersUC : UserControl
{

    private ArrayList myDataList = null;
    string currentItemText ;
    int currentItemIndex ;

    public SelectServersUC()
    {
        this.InitializeComponent();
        myDataList = LoadListBoxData();
        LeftListBox.ItemsSource = myDataList;  
    }
    private void UserControl_Loaded(object sender, RoutedEventArgs e)
    {
        myDataList = LoadListBoxData();
        LeftListBox.ItemsSource = myDataList;  
    }
    private ArrayList LoadListBoxData()
    {
        ArrayList itemsList = new ArrayList();
        itemsList.Add("Item1");
        itemsList.Add("Item2");
        itemsList.Add("Item3");
        itemsList.Add("Item4");
        itemsList.Add("Item5");
        itemsList.Add("Item6");
        itemsList.Add("Item7");
        itemsList.Add("Item8");
        itemsList.Add("Item9");
        itemsList.Add("Item10");
        return itemsList;
    }

    private void AddButton_Click(object sender, RoutedEventArgs e)
    {
        // Find the right item and it's value and index
        currentItemText = LeftListBox.SelectedValue.ToString();
        currentItemIndex = LeftListBox.SelectedIndex;

        RightListBox.Items.Add(currentItemText);
        if (myDataList != null)
        {
            myDataList.RemoveAt(currentItemIndex);
        }

        // Refresh data binding
        ApplyDataBinding();
    }

    private void RemoveButton_Click(object sender, RoutedEventArgs e)
    {
        // Find the right item and it's value and index
        currentItemText = RightListBox.SelectedValue.ToString();
        currentItemIndex = RightListBox.SelectedIndex;
        // Add RightListBox item to the ArrayList
        myDataList.Add(currentItemText);

       // LeftListBox.Items.Add(RightListBox.SelectedItem);
        RightListBox.Items.RemoveAt(RightListBox.Items.IndexOf(RightListBox.SelectedItem));

        // Refresh data binding
        ApplyDataBinding();
    }

    /// <summary>
    /// Refreshes data binding
    /// </summary>
    private void ApplyDataBinding()
    {
        LeftListBox.ItemsSource = null;
        // Bind ArrayList with the ListBox
        LeftListBox.ItemsSource = myDataList;
    }

2 个答案:

答案 0 :(得分:1)

答案 1 :(得分:0)

public partial class listtolist : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        for (int i = ListBox1.Items.Count - 1; i >= 0; i--)
        {
            if (ListBox1.Items[i].Selected)
            {
                ListBox2.Items.Add(ListBox1.Items[i]);
                ListBox1.Items.Remove(ListBox1.Items[i]);
            }
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        for (int i = ListBox2.Items.Count - 1; i >= 0; i--)
        {
            if (ListBox2.Items[i].Selected)
            {
                ListBox1.Items.Add(ListBox2.Items[i]);
                ListBox2.Items.Remove(ListBox2.Items[i]);
            }
        }
    }
}