WPF中MVVM模式的数据绑定例程

2016-10-07 19:09:06来源:CSDN作者:qq_32493189人点击

  • 首先,创建一个Student类
namespace listStudent{    public class Student    {        public int Id { get; set; }        public string Name { get; set; }        public int Age { get; set; }    }}
  • 建一个model
namespace listStudent.model{    public class ListModel:PropertyChangedBase    {        private ObservableCollection<Student> studentList = new ObservableCollection<Student>();        public ObservableCollection<Student> StudentList        {            get { return studentList; }            set            {                studentList = value;                NotifyOfPropertyChange(() => StudentList);            }        }        private Student selectedList;        public Student SelectedList        {            get { return selectedList; }            set            {                selectedList = value;                NotifyOfPropertyChange(() => SelectedList);            }        }    }}
  • 在主窗口中建立交互逻辑并初始化StudentList
namespace listStudent{    /// <summary>    /// MainWindow.xaml 的交互逻辑    /// </summary>    public partial class MainWindow : Window    {        public MainWindow()        {            InitializeComponent();            InitStudentList();            //this.lbStudent.ItemsSource = infos;            //this.txtStudentId.SetBinding(TextBox.TextProperty, new Binding("SelectedItem.Id") { Source = lbStudent });        }        ListModel signal = new ListModel();        void InitStudentList()        {            signal.StudentList.Clear();            signal.StudentList.Add(new Student() { Id = 1, Age = 11, Name = "Tom" });            signal.StudentList.Add(new Student() { Id = 2, Age = 12, Name = "Darren" });            signal.StudentList.Add(new Student() { Id = 3, Age = 13, Name = "Jacky" });            signal.StudentList.Add(new Student() { Id = 4, Age = 14, Name = "Andy" });           // signal.SelectedList = signal.StudentList[0];            sgve.DataContext = signal;        }    }}
  • 在UI(xaml)界面进行数据绑定
<Window x:Class="listStudent.MainWindow"        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        xmlns:vm="clr-namespace:listStudent"        x:Name="sgve"        Title="MainWindow" Height="350" Width="525">    <Grid>        <StackPanel Height="295" HorizontalAlignment="Left" Margin="10,10,0,0" Name="stackPanel1" VerticalAlignment="Top" Width="427">            <TextBlock Height="23" Name="textBlock1" Text="学员编号:" />            <TextBox Height="23" Name="txtStudentId" Width="301" HorizontalAlignment="Left" Text="{Binding SelectedList.Id}" />            <TextBlock Height="23" Name="textBlock2" Text="学员列表:" />            <ListBox Height="208" Name="lbStudent" Width="305" HorizontalAlignment="Left" SelectedItem="{Binding SelectedList}" ItemsSource="{Binding StudentList}">                <ListBox.ItemTemplate >                    <DataTemplate>                        <StackPanel Name="stackPanel2" Orientation="Horizontal">                            <TextBlock  Text="{Binding Id}" Margin="5" />                            <TextBlock Text="{Binding Name}" Margin="5"/>                            <TextBlock  Text="{Binding Age}" Margin="5"/>                        </StackPanel>                    </DataTemplate>                </ListBox.ItemTemplate>            </ListBox>        </StackPanel>    </Grid></Window>

本方法需要引入Caliburn.Micro.dll和Caliburn.Micro.Platform.dll框架

源码

框架dll

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台