I have the following class that I want to bind it’s dictionary’s value to a data grid control
public class DBRow : IEnumerable<DBColumn>
{
public DBColumn this[string ColumnName]
{
get { return Columns[ColumnName]; }
set { Columns[ColumnName] = value; }
}
public Dictionary<string, DBColumn> Columns { get; set; }
public DBRow()
{
this.Columns = new Dictionary<string, DBColumn>();
}
public List<KeyValuePair<string,DBColumn>> GetKeysPair()
{
List<KeyValuePair<string, DBColumn>> ListOfKeyPair = new List<KeyValuePair<string, DBColumn>>();
foreach (var KeyPair in Columns)
{
ListOfKeyPair.Add(KeyPair);
}
return ListOfKeyPair;
}
public void AddColumn(DBColumn Column)
{
Columns.Add(Column.Name, Column);
}
public IEnumerator<DBColumn> GetEnumerator()
{
foreach (var Column in Columns)
{
yield return Column.Value;
}
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
foreach (var item in Columns)
{
yield return item;
}
}
}
using foreach on the
GetKeysPair() method
I try to bind it like this:
foreach (var item in Row.GetKeysPair())
{
DataGridTextColumn DataGridColumn = new DataGridTextColumn();
DataGridColumn.Header = item.Value.Name;
DataGridColumn.Binding = new Binding(********); <<--- Problem!!
dataGridDataList.Columns.Add(DataGridColumn); <<--- DataGrid
}
Problem is that i don’t know what to type in
New Binding(string);
I am trying to bind the following property:
public struct DBColumn
{
public object Data { get; set; }
}
I tried alot of options but i just cant manage to figure it out 🙁
Hmm, I assumed the ItemsSource of your DataGrid is a collection of dictionaries (or a collection of list<keyvaluepair>), if this is the case then I believe you should be able to just use the indexer to access the value, e.g.