Reputation: 53
I am trying to bind a ListView
column to a property value of a DirectoryEntry
's Properties Collection in C# WPF
However I cannot seem to get the right syntax to access the following for one of my columns binding:
Properties["Department"].Value
Sample code below:
using System.DirectoryServices;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
namespace TestWPF
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void SetupColumns()
{
var gridView = new GridView();
this.listView.View = gridView;
gridView.Columns.Add(new GridViewColumn
{
Header = "Name",
DisplayMemberBinding = new System.Windows.Data.Binding("Name")
});
gridView.Columns.Add(new GridViewColumn
{
Header = "Department",
DisplayMemberBinding = new System.Windows.Data.Binding("Properties[\"Department\"].Value") //NEEDS TO GET Properties["Department"].Value for the binding
});
}
private void button_Click(object sender, RoutedEventArgs e)
{
SetupColumns();
DirectoryEntry directoryEntry = new DirectoryEntry();
DirectorySearcher searcher = new DirectorySearcher(directoryEntry)
{
PageSize = int.MaxValue,
Filter = "name=jbloggs"
};
List<DirectoryEntry> d1 = new List<DirectoryEntry>();
foreach (SearchResult t in searcher.FindAll())
{
d1.Add(t.GetDirectoryEntry());
}
DataContext = d1;
listView.ItemsSource = d1;
}
}
}
Any help would be much appreciated
Upvotes: 1
Views: 236
Reputation: 169200
Remove the escaped quotes from the binding path:
DisplayMemberBinding = new System.Windows.Data.Binding("Properties[Department].Value")
Upvotes: 1