2016-07-29 54 views
0

我正在將用戶從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)); 
    } 

回答

0

要顯示的用戶名,你必須設置ItemTemplate屬性:

<telerik:RadListBox x:Name="SearchListbox" Grid.Column="2" Grid.Row="2" 
    SelectionMode="Multiple" Margin="5 5 5 0" Height="100"> 
    <telerik:RadListBox.ItemTemplate> 
     <DataTemplate> 
      <TextBlock Text="{Binding Properties[cn]}"/> 
     </DataTemplate> 
    </telerik:RadListBox.ItemTemplate> 
</telerik:RadListBox> 

最佳做法是圍繞SearchResult模型創建一個ViewModel類。在這種情況下,您的視圖模型將宣佈一個FullName屬性,將訪問字典在模型中的實際值:

SearchResultViewModel.cs

public string FullName 
{ 
    get { return searchResult.Properties["cn"];} 
} 

你應該避免你的瀏覽綁到你的業務邏輯。使用上面的ViewModel,綁定看起來像Text="{Binding FullName}",這就消除了創建複雜綁定的必要性。

我不能使用的ItemSource,因爲我想從一個列表框 對象轉移到另一個用的ItemSource我不能簡單地從列表框中刪除對象 。

是的,你可以只使用一個ObservableCollection作爲ItemsSource。當您對此集合進行更改時,UI將會更新。

+0

我檢查了你的解決方案,但它不起作用。我有空的領域,像綁定沒有工作。我應該在CS中添加一些代碼來應用此綁定嗎? – Michael280

+0

我現在修復了綁定,它應該可以工作。 –

+0

是的,現在它的工作原理!感謝您的支持 – Michael280

相關問題