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#上位机17 小时前
wpf中Grid的MouseDown 事件无法触发的原因
c#·wpf
△曉風殘月〆18 小时前
如何在WPF中实现ComboBox多选
wpf
csdn_aspnet20 小时前
如何使用现有工具进行 .NET 8 迁移 Wpf
wpf·.net 8
点控云1 天前
点控云智能短信:重构企业与用户的连接,让品牌沟通更高效
大数据·人工智能·科技·重构·外呼系统·呼叫中心
123梦野2 天前
WPF——动画
wpf
攻城狮CSU2 天前
WPF 疑点汇总2.HorizontalAlignment和 HorizontalContentAlignment
wpf
c#上位机2 天前
wpf之命令
c#·wpf
没有bug.的程序员2 天前
分布式链路追踪:微服务可观测性的核心支柱
java·分布式·微服务·架构·wpf
萤丰信息2 天前
智慧园区:引领城市未来发展新趋势
大数据·科技·安全·重构·智慧城市·智慧园区
我的offer在哪里2 天前
2025 年 10 月科技前沿全景:从量子跃迁到生命重构的文明拐点
重构·量子计算