WPF笔记(三)MVVM

来源:互联网 时间:1970-01-01

一个简单的小例子,用来复习刚学的MVVM
项目结构

Model.BuyMessage代码

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace wpftest.Model{ public class BuyMessage { public int quantity { get; set; } public string name { get; set; } public string rate { get; set; } }}

ViewModel.MainViewModel代码

using System;using System.Collections.Generic;using System.Collections.ObjectModel;using System.Linq;using System.Text;using System.Threading.Tasks;namespace wpftest.ViewModel{ public class MainViewModel { public MainViewModel() { this.buyMessages = new ObservableCollection<Model.BuyMessage>(); this.buyMessages.Add(new Model.BuyMessage { quantity = 100, name="likai", rate="AA-" }); } public ObservableCollection<Model.BuyMessage> buyMessages { get; set; } }}

MainWindow.xaml

<Window x:Class="wpftest.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> <DataGrid Grid.Row="0" ItemsSource="{Binding buyMessages}" CanUserAddRows="False" AutoGenerateColumns="False"> <DataGrid.Columns > <DataGridTextColumn Width="220" Header="quantity" Binding="{Binding quantity}"></DataGridTextColumn> <DataGridTextColumn Width="220" Header="name" Binding="{Binding name}"></DataGridTextColumn> <DataGridTextColumn Width="220" Header="rate" Binding="{Binding rate}"></DataGridTextColumn> </DataGrid.Columns> </DataGrid> </Grid></Window>

运行结果

刚学没多久,徒手写这个小例子,权当温习一下



相关阅读:
Top