欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

WPF(Binding of LinQ)

程序员文章站 2022-04-14 11:00:28
[html]

[html]
<Window x:Class="TestOfLinQBinding.MainWindow" 
        xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml" 
        Title="MainWindow" Height="350" Width="525"> 
     
    <StackPanel Background="LightBlue" > 
        <ListView x:Name="listViewStudents" 
                  Height="143" 
                  Margin="5" > 
            <ListView.View > 
                <GridView > 
                    <GridViewColumn Header="Id" Width="60"  
                                    DisplayMemberBinding="{Binding Id}" /> 
                    <GridViewColumn Header="Name" Width="100" 
                                    DisplayMemberBinding="{Binding Name}" /> 
                    <GridViewColumn Header="Age" Width="80" 
                                    DisplayMemberBinding="{Binding Age}" /> 
                     
                </GridView> 
            </ListView.View> 
        </ListView> 
        <Button Content="Load" Height="25" Margin="5,0" Click="Button_Click" /> 
         
    </StackPanel> 
</Window> 

<Window x:Class="TestOfLinQBinding.MainWindow"
        xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
   
    <StackPanel Background="LightBlue" >
        <ListView x:Name="listViewStudents"
                  Height="143"
                  Margin="5" >
            <ListView.View >
                <GridView >
                    <GridViewColumn Header="Id" Width="60"
                                    DisplayMemberBinding="{Binding Id}" />
                    <GridViewColumn Header="Name" Width="100"
                                    DisplayMemberBinding="{Binding Name}" />
                    <GridViewColumn Header="Age" Width="80"
                                    DisplayMemberBinding="{Binding Age}" />
                   
                </GridView>
            </ListView.View>
        </ListView>
        <Button Content="Load" Height="25" Margin="5,0" Click="Button_Click" />
       
    </StackPanel>
</Window>
[csharp]
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 
 
namespace TestOfLinQBinding 

    /// <summary>  
    /// Interaction logic for MainWindow.xaml  
    /// </summary>  
    public partial class MainWindow : Window 
    { 
        public MainWindow() 
        { 
            InitializeComponent(); 
        } 
 
        private void Button_Click(object sender, RoutedEventArgs e) 
        { 
            List<Student> stuList = new List<Student>() 
            { 
                new Student(){Id = 0,Name = "Tim",Age = 29}, 
                new Student(){Id = 1,Name = "Tom",Age = 28}, 
                new Student(){Id = 2,Name = "Kyle",Age = 27}, 
                new Student(){Id = 3,Name = "Tony",Age = 26}, 
                new Student(){Id = 4,Name = "Vina",Age = 25}, 
                new Student(){Id = 5,Name = "Mike",Age = 24} 
            }; 
 
            this.listViewStudents.ItemsSource = from student in stuList where student.Name.StartsWith("T") select student ; 
 
        } 
    } 
 
    public class Student 
    { 
        public int Id { get; set; } 
        public String Name { get; set; } 
        public int Age { get; set; } 
    } 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace TestOfLinQBinding
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            List<Student> stuList = new List<Student>()
            {
                new Student(){Id = 0,Name = "Tim",Age = 29},
                new Student(){Id = 1,Name = "Tom",Age = 28},
                new Student(){Id = 2,Name = "Kyle",Age = 27},
                new Student(){Id = 3,Name = "Tony",Age = 26},
                new Student(){Id = 4,Name = "Vina",Age = 25},
                new Student(){Id = 5,Name = "Mike",Age = 24}
            };

            this.listViewStudents.ItemsSource = from student in stuList where student.Name.StartsWith("T") select student ;

        }
    }

    public class Student
    {
        public int Id { get; set; }
        public String Name { get; set; }
        public int Age { get; set; }
    }
}