在添加SearchResult对象时在ListBox中显示文本

时间:2022-12-04 20:33:47

I am adding Users from Active Directory to my ListBox. The object I got from Active Directory is SearchResult and this is what I add to my ListBox. The problem is I don't know how to display as a text value of SearchResult object property.

我将用户从Active Directory添加到我的ListBox。我从Active Directory获取的对象是SearchResult,这是我添加到ListBox的内容。问题是我不知道如何显示为SearchResult对象属性的文本值。

ListBox displays "System.DirectoryServices.SearchResult" while I would like to have displayed "John Smith" (which is "cn" property in my SearchResult object)

ListBox显示“System.DirectoryServices.SearchResult”,而我想显示“John Smith”(我的SearchResult对象中的“cn”属性)

Here is my code:

这是我的代码:

XAML:

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:

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

I can't use ItemSource because I want to transfer object from one ListBox to another and with ItemSource I can't simply delete object from ListBox.

我不能使用ItemSource,因为我想将对象从一个ListBox传输到另一个ListBox,而使用ItemSource我不能简单地从ListBox中删除对象。

Any idea how to handle that?

知道如何处理吗?

UPDATE, SOLVED PROBLEM WITH NOT CHANGING ObservableCollection:

更新,解决了没有改变ObservableCollection的问题:

full working code:

完整的工作代码:

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

1 个解决方案

#1


0  

To display the name of the user you have to set the ItemTemplate property:

要显示用户的名称,您必须设置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>

Best practice will be to create a ViewModel class around the SearchResult model. In this case your ViewModel will declare a FullName property that will access the dictionary in your model for the actual value:

最佳做法是围绕SearchResult模型创建一个ViewModel类。在这种情况下,ViewModel将声明一个FullName属性,该属性将访问模型中的字典以获取实际值:

SearchResultViewModel.cs

SearchResultViewModel.cs

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

You should avoid tying down your View to your business logic. Using the above ViewModel the binding will look like Text="{Binding FullName}", this removes the necessity to create complicated binding.

您应该避免将View与业务逻辑捆绑在一起。使用上面的ViewModel,绑定看起来像Text =“{Binding FullName}”,这消除了创建复杂绑定的必要性。

I cant use ItemSource because I want to transfer object from one ListBox to another and with itemSource I cant simply delete object from ListBox.

我不能使用ItemSource,因为我想将对象从一个ListBox转移到另一个ListBox,并且使用itemSource我不能简单地从ListBox中删除对象。

Yes you can, just use an ObservableCollection as ItemsSource. The UI will be updated when you make changes to this collection.

是的,你可以,只需使用ObservableCollection作为ItemsSource。当您对此集合进行更改时,将更新UI。

#1


0  

To display the name of the user you have to set the ItemTemplate property:

要显示用户的名称,您必须设置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>

Best practice will be to create a ViewModel class around the SearchResult model. In this case your ViewModel will declare a FullName property that will access the dictionary in your model for the actual value:

最佳做法是围绕SearchResult模型创建一个ViewModel类。在这种情况下,ViewModel将声明一个FullName属性,该属性将访问模型中的字典以获取实际值:

SearchResultViewModel.cs

SearchResultViewModel.cs

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

You should avoid tying down your View to your business logic. Using the above ViewModel the binding will look like Text="{Binding FullName}", this removes the necessity to create complicated binding.

您应该避免将View与业务逻辑捆绑在一起。使用上面的ViewModel,绑定看起来像Text =“{Binding FullName}”,这消除了创建复杂绑定的必要性。

I cant use ItemSource because I want to transfer object from one ListBox to another and with itemSource I cant simply delete object from ListBox.

我不能使用ItemSource,因为我想将对象从一个ListBox转移到另一个ListBox,并且使用itemSource我不能简单地从ListBox中删除对象。

Yes you can, just use an ObservableCollection as ItemsSource. The UI will be updated when you make changes to this collection.

是的,你可以,只需使用ObservableCollection作为ItemsSource。当您对此集合进行更改时,将更新UI。