WPF【11_6】WPF实战-重构与美化(MVVM 实战)-示例

--\ViewModels\MainViewModel.cs

public class MainViewModel

{

public List<Customer> Customers { get; set; } = new();

private Customer _selectedCustomer;

public Customer SelectedCustomer

{

get => _selectedCustomer; set

{

if (value != _selectedCustomer)

{

_selectedCustomer = value;

}

}

}

public void LoadCustomers()

{

using (var db = new AppDbContext())

{

Customers = db.Customers.Include(c => c.Appointments).ToList();

}

}

}

--\MainWindow.xaml.cs

public partial class MainWindow : Window

{

private MainViewModel _viewModel;

public MainWindow()

{

InitializeComponent();

_viewModel = new MainViewModel();

_viewModel.LoadCustomers();

DataContext = _viewModel;

}

}

--\MainWindow.xaml

<StackPanel Grid.Row="1" Grid.Column="0">

<Button Content="添加客户"/>

<ListView ItemsSource="{Binding Customers, Mode=OneWay}" DisplayMemberPath="Name" SelectedItem="{Binding SelectedCustomer, Mode=TwoWay}" />

</StackPanel>

<StackPanel Grid.Row="1" Grid.Column="1">

<TextBlock Text="姓名" Margin="10 10 10 0"/>

<TextBox Margin="10" Text="{Binding SelectedCustomer.Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

<TextBlock Text="身份证" Margin="10 10 10 0"/>

<TextBox Margin="10" Text="{Binding SelectedCustomer.IdNnumber, Mode=TwoWay}" />

<TextBlock Text="地址" Margin="10 10 10 0"/>

<TextBox Margin="10" Text="{Binding SelectedCustomer.Address, Mode=TwoWay}" />

<Button Content="保存" Margin="10 10 10 30" VerticalAlignment="Bottom" HorizontalAlignment="Left" />

</StackPanel>

<StackPanel Grid.Row="1" Grid.Column="2">

<ListView ItemsSource="{Binding SelectedCustomer.Appointments, Mode=TwoWay}" />

<TextBlock Text="添加新预约" />

<DatePicker Margin="10" />

<Button Content="预约" />

</StackPanel>

相关推荐
不是万能的豆学长20 小时前
游戏重构生活平衡新方式
游戏·重构·生活
云边有个稻草人21 小时前
MongoDB迁移不止是数据搬家:用KingbaseES重构文档数据开发
数据库·mongodb·重构·国产数据库·信创数据库·数据库国产化·mongodb迁移
热文新讯1 天前
海鸥医陪:以标准化力量,重构陪诊行业信任底色
重构
水无痕simon2 天前
3、工程重构
java·spring cloud·重构
avi91112 天前
电影开源Jevdo - 源码介绍(vue版)没有android 和 没有wpf
android·wpf·框架·数据爬虫·meego·电影管理
johnsong2 天前
AI前沿日报 · 2026-10-30 | 重构时刻——当基础设施开始重新洗牌
人工智能·重构
向上的车轮3 天前
合同去哪了,新合同从哪来?——软件行业付费逻辑重构
重构·fed·业务分析师
guoran_shini3 天前
AI开发与AI重构
重构·spec·持续迭代
隔窗听雨眠3 天前
从近端策略优化到群组相对策略优化:DeepSeek大模型强化学习训练范式重构
重构
sxgzzn3 天前
如何利用无人机提升光伏巡检效率?
运维·重构·无人机·开关柜局放在线监测·光伏运维管理系统