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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值