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>

相关推荐
thesky1234561 小时前
智能体面试准备(七十一):智能体系统的可演进架构与重构工程——接口契约、插件化与技术债治理
重构·插件化·智能体·技术债·接口契约·版本兼容·可演进架构
迪飞特科技1 小时前
分布式事务下 Spring 声明式事务失效的 3 种修复方案
分布式·spring·wpf
Likeadust2 小时前
多视角直播破局!企业融媒体系统在线高清直播EasyDSS多路同屏技术,重构实时可视化传播体验
人工智能·重构·媒体
weixin199701080162 天前
二手ERP对接闲鱼API:聚石塔强制入塔后的架构重构实录(正文骨架)
重构·架构
远航计算机2 天前
豆包”精采信”模式对企业官网运营有什么影响?2026年7月信源重构深度解读
人工智能·矩阵·重构·aigc·媒体
2601_962174173 天前
Redis 简介
数据库·redis·wpf
雪芽蓝域zzs3 天前
第三节:目录结构重构(仿若依,优化层级,layout 与 components 同级)
前端·vue.js·重构
蓝宝石的傻话3 天前
因为MiBeeNVR,决定接 onvif-go 自己管理并重构
数码相机·重构·golang
志栋智能3 天前
大模型驱动运维:重构故障根因分析的“思考”逻辑
运维·服务器·重构·自动化
深念Y3 天前
视频平台架构重构:从微服务到云原生
服务器·微服务·云原生·重构·架构·音视频·短视频