![](/img/trans.png)
[英]WPF How to Bind an Image to a DataGrid column as a property of the ItemsSource?
[英]WPF - DataGrid bind column to method instead of property
我有一個表示頂點的類,該頂點可以具有多個屬性。 每個屬性均由其名稱標識。
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;
}
}
我想要一個DataGrid
,它將其ItemsSource
綁定到某些ObservableCollection<Vertex>
。 每一列應代表頂點的一個屬性。 我想在運行時控制列(添加新列或刪除舊列)。 但是我無法做到這一點,因為新的列綁定需要從中獲取單元格數據的屬性的名稱。 但是我需要使用屬性名稱來綁定調用方法GetAttributeByName()
來獲取數據,而不僅僅是獲取屬性。
string newAttributeName = "some_name";
DataGridTextColumn newColumn = new DataGridTextColumn();
newColumn.Header = newAttributeName;
newColumn.Binding = ???;
dataGrid.Columns.Add(newColumn);
有什么辦法嗎?
有什么辦法嗎?
不,您不能真正綁定到這樣的方法。
但是,如果將Dictionary<string, Attr>
公開為Vertex
類的公共屬性,則應該可以綁定到此:
string newAttributeName = "some_name";
DataGridTextColumn newColumn = new DataGridTextColumn();
newColumn.Header = newAttributeName;
newColumn.Binding = new Binding($"Attributes[{newAttributeName}]");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.