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>

相关推荐
小程故事多_8010 小时前
从人工编写到自主迭代进化,SkillEvolver重构大模型智能体技能生成新范式
人工智能·重构
逸模13 小时前
AI+BIM 重构连锁公装新范式 逸模打造数字化营建核心底座
大数据·人工智能·笔记·其他·信息可视化·重构
weixin_5498083616 小时前
从“大海捞针“到“精准定位“:易薪路AI人才罗盘如何用AI重构企业人才选拔与组织发展
大数据·人工智能·重构
Python私教16 小时前
用 Claude Code 做大型重构不翻车:分批+Git 兜底+验证闭环的实战流程(2026)
git·重构·ai编程·代码重构·工程实践·claude code
在水一缸16 小时前
告别“黑盒”依赖:从英国内政部难民系统重构看政府数字化转型的自研之路
重构·saas·数字化转型·技术决策·自研系统·政府科技·遗留系统现代化
ANnianStriver17 小时前
PetLumina 03 — 后端目录重构与 Web 管理后台搭建
java·前端·ai·重构·ai编程·claude code
m0_4665252918 小时前
KTOS重构产业生态:酷特智能打造企业数智化新基建
人工智能·重构
kv0000718 小时前
项目工作量评估电子化:重构项目管理效率的全流程实践
科技·重构·项目管理
JAMSAN093021 小时前
通信权力的去中心化重构:Web3通讯工具市场深度分析
重构·web3·去中心化·交互
知行产研2 天前
红二矿:深耕能源融合与智能重构 探索矿山低碳高效转型新路径
大数据·重构·能源