C# 如何将多个选定项从一个列表框添加/移动到另一个列表框?
我有一个小的原型,可以将选定的项目从一个列表框添加到另一个列表框。我需要能够从列表框中选择多个项目,并将它们移动到另一个列表框,然后向后移动,从第二个列表框移动到第一个列表框。我想知道是否有人有很好的样本或可以修改我已经有代码。先谢谢你 两个列表框和按钮XAML:C# 如何将多个选定项从一个列表框添加/移动到另一个列表框?,c#,wpf,wpf-controls,C#,Wpf,Wpf Controls,我有一个小的原型,可以将选定的项目从一个列表框添加到另一个列表框。我需要能够从列表框中选择多个项目,并将它们移动到另一个列表框,然后向后移动,从第二个列表框移动到第一个列表框。我想知道是否有人有很好的样本或可以修改我已经有代码。先谢谢你 两个列表框和按钮XAML: <Grid x:Name="LayoutRoot"> <Grid.ColumnDefinitions> <ColumnDefinition/> </Grid.C
<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;
}
公共部分类SelectServersUC:UserControl
{
private ArrayList myDataList=null;
字符串currentItemText;
int-currentItemIndex;
公共选择服务器suc()
{
this.InitializeComponent();
myDataList=LoadListBoxData();
LeftListBox.ItemsSource=myDataList;
}
已加载私有void UserControl_(对象发送方,RoutedEventArgs e)
{
myDataList=LoadListBoxData();
LeftListBox.ItemsSource=myDataList;
}
专用ArrayList LoadListBoxData()
{
ArrayList itemsList=新的ArrayList();
项目列表添加(“项目1”);
项目列表添加(“项目2”);
项目列表添加(“项目3”);
项目列表。添加(“项目4”);
项目列表。添加(“项目5”);
项目列表添加(“项目6”);
项目列表添加(“项目7”);
项目列表添加(“项目8”);
项目列表添加(“项目9”);
项目列表。添加(“项目10”);
返回项目列表;
}
私有void AddButton_单击(对象发送方,路由目标)
{
//找到正确的项及其值和索引
currentItemText=LeftListBox.SelectedValue.ToString();
currentItemIndex=LeftListBox.SelectedIndex;
RightListBox.Items.Add(currentItemText);
如果(myDataList!=null)
{
myDataList.RemoveAt(currentItemIndex);
}
//刷新数据绑定
ApplyDataBinding();
}
私有无效删除按钮\单击(对象发送方,路由目标)
{
//找到正确的项及其值和索引
currentItemText=RightListBox.SelectedValue.ToString();
currentItemIndex=RightListBox.SelectedIndex;
//将RightListBox项添加到ArrayList
添加(currentItemText);
//LeftListBox.Items.Add(RightListBox.SelectedItem);
RightListBox.Items.RemoveAt(RightListBox.Items.IndexOf(RightListBox.SelectedItem));
//刷新数据绑定
ApplyDataBinding();
}
///
///刷新数据绑定
///
私有void ApplyDataBinding()
{
LeftListBox.ItemsSource=null;
//将ArrayList与ListBox绑定
LeftListBox.ItemsSource=myDataList;
}
看看这个
看看这个
我需要能够移动多个项目。来自c-sharpcorner的示例介绍了如何移动单个项目的技术。来自c-sharpcorner的示例介绍了如何将单个项目从左列表框向右列表框单向移动的技术。我正在寻找允许从左列表框向两个方向移动项目的示例stBox到RightListBox,再从RightListBox到LeftListBox,以启用向后交互。我需要能够移动多个项目。c-sharpcorner的示例介绍了如何移动单个项目的技术。c-sharpcorner的示例介绍了如何将单个项目从左listbox向左listbox移动的技术我正在寻找一个示例,它允许在两个方向上将项目从LeftListBox移动到RightListBox,并从RightListBox移动回LeftListBox,以实现向后交互。
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]);
}
}
}
}