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>

相关推荐
homelook3 小时前
【论文解读】可重构电池短路与最短路径分析
重构
桐秋廿3 小时前
【光学仿真】02:PSI Algorithm:基于Fringe索引的Zernike拟合全流程——从干涉图仿真到波前重构
重构
笨蛋不要掉眼泪7 小时前
Spring Boot + RedisTemplate 数据结构的基础操作
java·数据结构·spring boot·redis·wpf
言無咎1 天前
从规则引擎到任务规划:AI Agent 重构跨境财税复杂账务处理体系
大数据·人工智能·python·重构
程序猿阿伟1 天前
《非暴力通关的深度策略与挑战重构手册》
重构
LcVong1 天前
WPF MediaPlayer获取网络视频流当前帧并展示图片完整范例
网络·wpf
bugcome_com1 天前
WPF数据绑定入门:从传统事件到5种绑定模式
wpf
LateFrames1 天前
我用 WPF 做了一个 “苍蝇飞舞” 的屏保
ui·wpf
中國龍在廣州1 天前
AI时代“新BAT”正在崛起
大数据·人工智能·深度学习·重构·机器人
wuty0072 天前
完善基于WPF开发的标尺控件(含实例代码)
wpf·wpf标尺·支持横向竖向标尺·ruler