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>

相关推荐
c#上位机20 分钟前
wpf之TextBlock
c#·wpf
迦蓝叶1 天前
JAiRouter 配置文件重构纪实 ——基于单一职责原则的模块化拆分与内聚性提升
java·网关·ai·重构·openai·prometheus·单一职责原则
程序员的世界你不懂1 天前
【Flask】测试平台开发,重构提测管理页面-第二十篇
vue.js·重构·flask
老猿讲编程1 天前
存算一体:重构AI计算的革命性技术(1)
人工智能·重构
玉面小君1 天前
从 WPF 到 Avalonia 的迁移系列实战篇6:ControlTheme 和 Style区别
c#·wpf·avalonia
c#上位机2 天前
wpf之Border
c#·wpf
SunflowerCoder2 天前
WPF迁移avalonia之图像处理(一)
图像处理·wpf·avalonia
周杰伦fans2 天前
WPF中的DataContext以及常见的绑定方式
wpf
CHEN5_022 天前
【CouponHub项目开发】使用RocketMQ5.x实现延时修改优惠券状态,并通过使用模板方法模式重构消息队列发送功能
java·重构·模板方法模式·项目
萤丰信息3 天前
智慧工地如何撕掉“高危低效”标签?三大社会效益重构建筑业价值坐标
java·大数据·人工智能·微服务·重构·架构·智慧工地