简单地说,我的DataGrid
XML 文件中有一个绑定到一个列表的文件——在这个例子中,是一个Person
s 的列表。但是,我还想要一个额外的列,因为此列表中的所有条目都将是 的子类Person
,即PersonWithAge
. 不过,我不允许更改 的类型ListOfPeople
。
如果我在 XAML 中手动添加该列,它会立即崩溃。如何添加此列并将其与所有其他列同步?有没有办法在 XAML 文件中转换它?或者可以在m_grid_AutoGeneratingColumn
方法中添加一个?
这是我的代码:
// In MyGridView.xaml.cs
public class MyGridView
{
public ObservableCollection<Person> ListOfPeople { get; set; }
private void m_grid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
/* Some formatting here... */
}
}
public class Person
{
public string Name { get; set; }
}
public class PersonWithAge : Person
{
public int Age{ get; set; }
}
XAML 代码:
// MyGridView.xaml
<UserControl x:Class="Project.MyGridView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<DataGrid Name="m_myGrid"
ItemsSource="{Binding ListOfPeople}
AutoGenerateColumns="True" AutoGeneratingColumn="m_grid_AutoGeneratingColumn" >
<!--DataGridTextColumn Header="Age" /--> <!-- This makes the thing crash -->
</DataGrid >
</Grid>
</UserControl>