--\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>