如何根据选择将列表绑定到数据网格并查看内容

问题描述:

我有3个学生的列表,其中包含详细信息。我想将列表绑定到datagrid。我可以这样做。以下是我的代码,xaml.please给我更正:







i have a list of 3 students with details in it.i want to bind the list to datagrid.how can i do that.Following is my code behind and xaml.please give me corrections:



public partial class MainWindow : Window
   {



       private List<student> _personaldetails = new List<student>();

       public MainWindow()
       {
           InitializeComponent();
           _personaldetails.Add(new student { name = "qqq", id = "qqq", address = "qqq" });
           _personaldetails.Add(new student { name = "www", id = "www", address = "www" });
           _personaldetails.Add(new student { name = "eee", id = "eee", address = "eee" });



}













xaml:







< datagrid autogeneratecolumns =Trueheight =311horizo​​ntalalignment = leftmargin =281,0,0,0name =dataGrid1verticalalignment =Topwidth =222itemssource ={Binding _personaldetails}>

< datagrid .columns>

< datagridtextcolumn header =Namewidth =75canuserresize =Falsecanuserreorder =Falseisreadonly =True>

&lt ; datagridtextcolumn header =Idwidth =75canuserresize =Falsecanuserreorder =Falseisreadonly =True>

< datagridtextcolumn header =Addresswidth =75canuserresize =Falsecanuserreorder =Falseisreadonly =True>



}






xaml:



<datagrid autogeneratecolumns="True" height="311" horizontalalignment="Left" margin="281,0,0,0" name="dataGrid1" verticalalignment="Top" width="222" itemssource="{Binding _personaldetails}">
<datagrid.columns>
<datagridtextcolumn header="Name" width="75" canuserresize="False" canuserreorder="False" isreadonly="True">
<datagridtextcolumn header="Id" width="75" canuserresize="False" canuserreorder="False" isreadonly="True">
<datagridtextcolumn header="Address" width="75" canuserresize="False" canuserreorder="False" isreadonly="True">

将集合绑定到数据网格



Bind the collection to the datagrid

ItemsSource="{Binding __personaldetails}"





获取WPF书籍并完成示例,下载一些初学者文章并看看它们。



Get book on WPF and work through the examples, download some of the beginner articles and take a look at them.