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>

相关推荐
陈喜标bill15 小时前
S2B2C私域会员电商如何重构企业经营逻辑
大数据·人工智能·重构
newrank_kk15 小时前
AI 搜索时代新战场:智汇GEO 如何重构品牌 AI 形象管理规则
人工智能·重构
ezreal_pan16 小时前
基于券类型路由的渐进式重构:函数式选项模式与管道模式的完美结合
设计模式·重构·golang·选项函数
有味道的男人16 小时前
国内电商 API 深度赋能:从选品、库存到履约,重构电商运营效率新范式
大数据·重构
AI科技星17 小时前
质量定义方程的物理数学融合与求导验证
数据结构·人工智能·算法·机器学习·重构
GEO AI搜索优化助手17 小时前
未来图景:信息传播链的生态重构与长期影响
人工智能·搜索引擎·重构·生成式引擎优化·ai优化·geo搜索优化
跨境摸鱼19 小时前
TikTok多账号风控:找对安全支点,解锁规模化运营
大数据·安全·矩阵·重构·跨境电商
没有bug.的程序员19 小时前
SOA、微服务、分布式系统的区别与联系
java·jvm·微服务·架构·wpf·日志·gc
老蒋新思维20 小时前
创客匠人:工作流嵌入式智能体,重构知识变现的效率底层
大数据·服务器·人工智能·重构·创始人ip·创客匠人·知识变现
Macbethad20 小时前
基于WPF的半导体设备配方管理程序技术方案
wpf