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>

相关推荐
Microvision维视智造10 小时前
从“人工眼”到‘智能眼’:EZ-Vision视觉系统如何重构生产线视觉检测精度?
图像处理·人工智能·重构·视觉检测
界面开发小八哥12 小时前
界面组件DevExpress WPF中文教程:Grid - 如何过滤节点?
.net·wpf·界面控件·devexpress·ui开发
I'mSQL12 小时前
C#与WPF使用mvvm简单案例点击按钮触发弹窗
开发语言·c#·wpf
速易达网络13 小时前
[特殊字符]️ 风暴之眼:AI时代IT从业者的重构与重生
人工智能·重构
博闻录16 小时前
以 “有机” 重构增长:云集从电商平台到健康生活社区的跃迁
大数据·重构·生活
大白玉米1 天前
TVBOXOS6.0双端APP二开源码完整版全开源源码重构版
java·重构·php
百锦再2 天前
WPF依赖属性深度解析:从原理到高级应用
wpf·依赖·绑定·验证·net·强制
✎ ﹏梦醒͜ღ҉繁华落℘2 天前
WPF高级学习(一)
学习·wpf
界面开发小八哥2 天前
界面控件DevExpress WPF v25.1新版亮点:模板库更新升级
ui·.net·wpf·界面控件·devexpress·ui开发
△曉風殘月〆2 天前
WPF MVVM进阶系列教程(二、数据验证)
wpf·mvvm