WPF ListView绑定小例子

2 篇文章 0 订阅
 <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绑定的小例子。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值