我正在將用戶從Active Directory添加到我的ListBox
。 我從Active Directory獲得的對象是SearchResult
,這就是我添加到我的ListBox
。問題是我不知道如何顯示SearchResult對象屬性的文本值。在添加SearchResult對象時在ListBox中顯示文本
ListBox
顯示「System.DirectoryServices.SearchResult」,而我想有顯示爲「約翰·史密斯」(這是我的信息搜索結果對象「CN」屬性)
這裏是我的代碼:
XAML :
<telerik:RadListBox Grid.Column="2" Grid.Row="2" SelectionMode="Multiple" x:Name="SearchListbox" Margin="5 5 5 0" Height="100"/>
<telerik:RadListBox Grid.Column="4" Grid.Row="2" SelectionMode="Multiple" x:Name="AddListbox" Margin="5 5 5 0" Height="100"/>
CS:
DirectorySearcher searcher = new DirectorySearcher(entry.Path)
{
Filter = "(&(cn*)(sn=*)(mail=*)(givenName=*))"
};
var results = searcher.FindAll();
foreach (SearchResult result in results)
{
SearchListBox.Items.Add(result);
}
我不能使用ItemSource
,因爲我想將對象從一個ListBox
轉移到另一個,並且使用ItemSource
我不能簡單地從ListBox
刪除對象。
任何想法如何處理?
最新情況,在不改變的ObservableCollection解決的問題:
完整的工作代碼:
private ObservableCollection<SearchResult> resultsSearch = new ObservableCollection<SearchResult>();
private ObservableCollection<SearchResult> resultsAdd = new ObservableCollection<SearchResult>();
public ObservableCollection<SearchResult> ResultsSearch
{
get { return resultsSearch; }
set { resultsSearch = value; }
}
public ObservableCollection<SearchResult> ResultsAdd
{
get { return resultsAdd; }
set { resultsAdd = value; }
}
public event PropertyChangedEventHandler PropertyChanged;
public event NotifyCollectionChangedEventHandler CollectionChanged;
private void NotifyPropertyChanged(string property)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
protected virtual void OnCollectionChange(NotifyCollectionChangedEventArgs e)
{
if (CollectionChanged != null)
CollectionChanged(this, e);
}
public void Add(SearchResult item)
{
this.ResultsSearch.Add(item);
this.OnCollectionChange(
new NotifyCollectionChangedEventArgs(
NotifyCollectionChangedAction.Add, item));
}
public void Remove(SearchResult item)
{
this.ResultsSearch.Remove(item);
this.OnCollectionChange(
new NotifyCollectionChangedEventArgs(
NotifyCollectionChangedAction.Remove, item));
}
我檢查了你的解決方案,但它不起作用。我有空的領域,像綁定沒有工作。我應該在CS中添加一些代碼來應用此綁定嗎? – Michael280
我現在修復了綁定,它應該可以工作。 –
是的,現在它的工作原理!感謝您的支持 – Michael280