[英]WPF DataGrid binding not working
I cannot get DataGrid binding to work in the example bellow. 我无法在下面的示例中使用DataGrid绑定。 Any clues on what's going on ?
有什么线索吗?
namespace WPFTestApplication
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public class Person
{
public int age { get; set; }
public String Name { get; set; }
public Person(int age, String Name)
{
this.age = age;
this.Name = Name;
}
}
public class MegaObject
{
public IList<Person> persons { get; set; }
public MegaObject()
{
persons = new List<Person>();
persons.Add(new Person(11, "A"));
persons.Add(new Person(12, "B"));
persons.Add(new Person(13, "C"));
}
}
public Window1()
{
InitializeComponent();
MegaObject myobject= new MegaObject();
DataContext = myobject;
}
}
}
<Grid>
<my:DataGrid
Name="dataGrid"
AutoGenerateColumns="False"
ItemsSource="{Binding Source=persons}"
>
<my:DataGrid.Columns>
<my:DataGridTextColumn Binding="{Binding Path=age, Mode=TwoWay}" >
</my:DataGridTextColumn>
<my:DataGridTextColumn Binding="{Binding Path=Name, Mode=TwoWay}" >
</my:DataGridTextColumn>
</my:DataGrid.Columns>
</my:DataGrid>
</Grid>
Regards, MadSeb 此致MadSeb
The ItemsSource binding needs to have Path set, not Source, to persons
. ItemsSource绑定需要对
persons
设置Path而不是Source。 Simply putting it as {Binding persons}
would do the trick (Path is the default property in markup) or explicitly {Binding Path=persons}
. 简单地将其设置为
{Binding persons}
(Path是标记中的默认属性)或显式使用{Binding Path=persons}
。 The DataContext is always inherited. DataContext始终是继承的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.