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

WPF ListView绑定小例子

程序员文章站 2022-03-07 17:03:49
...
 <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="50"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <StackPanel Grid.Row="0" Orientation="Horizontal">
            <TextBlock Text="编号" VerticalAlignment="Center"></TextBlock>
            <TextBox Text="{Binding SelectStudent.Index}" Width="100" Height="30" VerticalAlignment="Center" VerticalContentAlignment="Center"></TextBox>
            <TextBlock Text="姓名" VerticalAlignment="Center"></TextBlock>
            <TextBox Text="{Binding SelectStudent.Name}" Width="100" Height="30" VerticalContentAlignment="Center" VerticalAlignment="Center"></TextBox>
        </StackPanel>
        <ListView Grid.Row="1"  x:Name="visionAlarmList"
                    ItemsSource="{Binding Students}" 
                    IsSynchronizedWithCurrentItem="True"
                   SelectedItem="{Binding SelectStudent}"
                   >
            <ListView.ItemContainerStyle>
                <Style TargetType="ListViewItem">
                    <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                </Style>
            </ListView.ItemContainerStyle>
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="编号"  Width="60"  DisplayMemberBinding="{Binding Index}"/>
                    <GridViewColumn Header="姓名"  Width="80"  DisplayMemberBinding="{Binding Name}" />
                    
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>

xaml部分

 

using Microsoft.Practices.Prism.ViewModel;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WpfApplication1
{
    public class Student : NotificationObject
    {
        private int index;

        public int Index
        {
            get { return index; }
            set { index = value; this.RaisePropertyChanged("Index"); }
        }
        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; this.RaisePropertyChanged("Name"); }
        }
    }
    public class MainViewModel : NotificationObject
    {
        private Student selectStudent;

        public Student SelectStudent
        {
            get { return selectStudent; }
            set { selectStudent = value; this.RaisePropertyChanged("SelectStudent"); }
        }
        private ObservableCollection<Student> students;

        public ObservableCollection<Student> Students
        {
            get { return students; }
            set { students = value; this.RaisePropertyChanged("Students"); }
        }

        public MainViewModel()
        {
            this.students = new ObservableCollection<Student>();
            Student student = new Student() { Index = 1, Name = "张三" };
            this.Students.Add(student);
            student = new Student() { Index = 2, Name = "李四" };
            this.Students.Add(student);   
        }
    }
}

ViewModel部分

以上就是一个利用微软Prism库写的WPF ListView绑定的小例子。