I believe everyone has met this demand, the DataGrid shows a list of students matching the screening criteria, the above Textbolck shows the total number of students, you can do this:
1,xaml Code
1<window x:class="Observablecollectiondemo.mainwindow"2xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"3xmlns:x="Http://schemas.microsoft.com/winfx/2006/xaml"4xmlns:observablecollectiondemo="Clr-namespace:observablecollectiondemo"5title="MainWindow"6height=" -"7Width="525">8<Window.DataContext>9<observablecollectiondemo:mainwindowviewmodel/>Ten</Window.DataContext> One<Grid> A<Grid.RowDefinitions> -<rowdefinition height="Auto"/> -<rowdefinition/> the</Grid.RowDefinitions> -<TextBlock> Total Students: <run text="{Binding students.count, mode=oneway}"/></textblock> -<datagrid grid.row="1" -Itemssource="{Binding Students}"/> +</Grid> -</Window>
2,. CS Code
1 Public Partial classMainwindow:window2 {3 PublicMainWindow ()4 {5 InitializeComponent ();6 }7 }8 9 Public classStudentTen { One Public stringName {Get;Set; } A Public intAge {Get;Set; } - } - the Public classMainwindowviewmodel - { - PublicObservablecollection<student> Students {Get;Set; } - + PublicMainwindowviewmodel () - { + This. Students =NewObservablecollection<student>(); A for(inti =0; I <5; i++) at { - This. Students.add (NewStudent () - { -Name =string. Format ("Student {0}", i), -Age =Ten+I - }); in } - } to}
3, Operation effect
The Count property of the WPF binding ObservableCollection