尝试移动和项目或多个,如果从一个列表框中选择到另一个.左边的列表框中填充了从AD搜索的用户.然后我想选择一个或多个,并将它们移动到右边的列表框.我的问题是我不能移动它们.下面的代码是搜索和移动功能的功能.我是新的WPF,所以学习和发挥得到一些理解.希望有人能在这方面提供帮助.
function SearchDGUserClick
{
param($sender, $e)
$DGMemberSearchListBox.ItemsSource
$query = $DGUserSearchText.Text
$UserSearch = (Get-ADUser -Filter "Name -like '*$query*'") | Select SamAccountName | Sort -Property SamaAccountName
$DGMemberSearchListBox.ItemsSource = $UserSearch
}
function AddUserClick
{
param($sender, $e)
#$UserSearch should be an array or collection of user objects
$DGMemberSearchListBox.ItemsSource = $UserSearch
#Handle the button click or any other event that triggers the move
$selectedItems = @()
foreach ($item in $DGMemberSearchListBox.SelectedItems) {
#If DGMemberAddListBox doesn't have the item yet, add it
if ($DGMemberAddListBox.Items -notcontains $item) {
$DGMemberAddListBox.Items.Add($item)
}
#Remove the item from DGMemberSearchListBox
$selectedItems += $item
}
#Remove the selected items from DGMemberSearchListBox
foreach ($item in $selectedItems) {
$DGMemberSearchListBox.Items.Remove($item)
}
}
字符串
尝试玩这个以及,但当我运行它,它搜索用户罚款我可以选择一个用户,当我点击移动它清除左侧,并没有填充的权利。
先谢了。
1条答案
按热度按时间afdcj2ne1#
在
ListBox
的Items
集合中添加或删除项时,不应使用ItemsSource
属性。这是一种方法,但不是两种方法。试试这个:
字符串
假设您在将项目移动到
DGMemberAddListBox
之前首先填充DGMemberSearchListBox
,它应该可以工作。