Reputation: 186
I have a class that represents a vertex, which can have multiple attributes. Each attribute is identified by its name.
public class Vertex
{
private Dictionary<string, Attr> attributes;
public Attr GetAttributeByName(string attributeName)
{
Attr attribute;
if (attributes.TryGetValue(attributeName, out attribute))
{
return attribute;
}
return null;
}
}
I want to have a DataGrid
that will have its ItemsSource
bound to some ObservableCollection<Vertex>
. Each column should represent one attribute of the vertex. And I want to control the columns at runtime (adding new columns or removing old ones). But I am not able to do that, because new column binding wants the name of property that it will get the cell data from. But I need the binding to call method GetAttributeByName()
with the name of the attribute to get the data, rather than just getting a property.
string newAttributeName = "some_name";
DataGridTextColumn newColumn = new DataGridTextColumn();
newColumn.Header = newAttributeName;
newColumn.Binding = ???;
dataGrid.Columns.Add(newColumn);
Is there some way to do it?
Upvotes: 1
Views: 1011
Reputation: 169190
Is there some way to do it?
No, you can't really bind to methods like this.
But if you expose the Dictionary<string, Attr>
as a public property of the Vertex
class, you should be able to bind to this one:
string newAttributeName = "some_name";
DataGridTextColumn newColumn = new DataGridTextColumn();
newColumn.Header = newAttributeName;
newColumn.Binding = new Binding($"Attributes[{newAttributeName}]");
Upvotes: 3