本文介绍了WPF DataGrid绑定不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在下面的示例中,我无法使DataGrid绑定正常工作.有什么线索吗?
I cannot get DataGrid binding to work in the example bellow.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>
关于,MadSeb
推荐答案
ItemsSource绑定需要将Path(而非Source)设置为 persons
.简单地将其表示为 {Binding person}
就可以解决问题(Path是标记中的默认属性)或显式地 {Binding Path = persons}
.DataContext始终是继承的.
The ItemsSource binding needs to have Path set, not Source, to persons
. Simply putting it as {Binding persons}
would do the trick (Path is the default property in markup) or explicitly {Binding Path=persons}
. The DataContext is always inherited.
这篇关于WPF DataGrid绑定不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!