正在尝试从WPF powershell项目列表框中移动项

lpwwtiir  于 11个月前  发布在  Shell
关注(0)|答案(1)|浏览(100)

尝试移动和项目或多个,如果从一个列表框中选择到另一个.左边的列表框中填充了从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)
}

}

字符串
尝试玩这个以及,但当我运行它,它搜索用户罚款我可以选择一个用户,当我点击移动它清除左侧,并没有填充的权利。
先谢了。

afdcj2ne

afdcj2ne1#

ListBoxItems集合中添加或删除项时,不应使用ItemsSource属性。这是一种方法,但不是两种方法。
试试这个:

function SearchDGUserClick
{
  param($sender, $e)

  $query = $DGUserSearchText.Text
  $UserSearch = (Get-ADUser -Filter "Name -like '*$query*'") | Select SamAccountName | Sort -Property SamaAccountName

  foreach ($item in $UserSearch) {
    $DGMemberSearchListBox.Items.Add($item)
  }
}

function AddUserClick
{
  param($sender, $e)

  #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)
  }
}

字符串
假设您在将项目移动到DGMemberAddListBox之前首先填充DGMemberSearchListBox,它应该可以工作。

相关问题