WPF DataGrid绑定不起作用

编程入门 行业动态 更新时间:2024-10-27 21:23:07
本文介绍了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绑定不起作用

本文发布于:2023-11-10 06:01:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1574538.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   不起作用   WPF   DataGrid

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!