WPF DataGrid binding not working
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;
  开发者_StackOverflow中文版              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
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.
 
         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论