MVVM绑定

<Page x:Class="ERPApp.View.ProductsView"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:local="using:ERPApp.View"
      mc:Ignorable="d" 
      d:DesignHeight="450" d:DesignWidth="800"
      Title="ProductsView">

    <Page.DataContext>
        <Binding Path="Products" Source="{StaticResource Locator}"></Binding>
    </Page.DataContext>
    
    <Grid >
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
        </Grid.RowDefinitions>

        <TextBlock Grid.Row="0">Products:</TextBlock>

        <DataGrid x:Name="dataGridProducts" Grid.Row="1" AutoGenerateColumns="False" SelectedItem="{Binding SelectedProduct}" ItemsSource="{Binding Path=Products}">
            <DataGrid.Columns>
                <DataGridTextColumn x:Name="idColumn" Binding="{Binding Id}" Header="Id" Width="80"/>
                <DataGridTextColumn x:Name="nameColumn" Binding="{Binding Name}" Header="Name" Width="80"/>
                <DataGridTextColumn x:Name="remarkColumn" Binding="{Binding Remark}" Header="Remark" Width="120"/>
                <DataGridTemplateColumn>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <Button Content="编辑" Command="{Binding DataContext.UpdateProductCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Page}}"/>
                                <Button Content="删除" Command="{Binding DataContext.DeleteProductCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Page}}"/>
                            </StackPanel>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>

        <Grid Grid.Row="3" >
            <Grid.ColumnDefinitions>
                <ColumnDefinition></ColumnDefinition>
                <ColumnDefinition></ColumnDefinition>
                <ColumnDefinition></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Button Grid.Column="0" Command="{Binding CreateProductCommand}">添加</Button>
            <Button Grid.Column="1" Command="{Binding UpdateProductCommand}">修改</Button>
            <Button Grid.Column="2" Command="{Binding DeleteProductCommand}">删除</Button>
        </Grid>
    </Grid>
</Page>


public class ProductsViewModel : ObservableRecipient
{
    private ViewModelLocator locator;
    private ERPModelContainer _context;

    public ObservableCollection<Product> Products { get; private set; }

    public Product SelectedProduct { get; set; }


    #region RelayCommand

    public RelayCommand CreateProductCommand { get; private set;}
    public RelayCommand UpdateProductCommand { get; private set; }
    public RelayCommand DeleteProductCommand { get; private set; }

    #endregion

    public ProductsViewModel()
    {
        locator = (ViewModelLocator)Application.Current.Resources["Locator"];

        _context = locator.ERPModelContainer;
        _context.Products.Load();
        Products = _context.Products.Local;

        CreateProductCommand = new RelayCommand(ExecuteCreateProduct);
        UpdateProductCommand = new RelayCommand(ExecuteUpdateProduct);
        DeleteProductCommand = new RelayCommand(ExecuteDeleteProduct);
    }

    public void ExecuteCreateProduct()
    {
        ProductWindow productWindow = new ProductWindow();
        {
            WeakReferenceMessenger.Default.Send(new Product());
            if (productWindow.ShowDialog() == true)
            {
                if (!string.IsNullOrEmpty(locator.Product.Product.Name))
                {
                    _context.Products.Add(locator.Product.Product);
                    _context.SaveChanges();
                }
            }
        }
    }

    public void ExecuteUpdateProduct()
    {
        ProductWindow productWindow = new ProductWindow();
        {
            WeakReferenceMessenger.Default.Send(new Product());
            if (productWindow.ShowDialog() == true)
            {
                if (!string.IsNullOrEmpty(locator.Product.Product.Name))
                {
                    _context.Products.Add(locator.Product.Product);
                    _context.SaveChanges();
                }
            }
        }
    }

    public void ExecuteDeleteProduct()
    {
        if (SelectedProduct != null)
        {
            if(MessageBox.Show($"你确定要删除Id={SelectedProduct.Id}这一行吗?", "确认", MessageBoxButton.YesNo)== MessageBoxResult.Yes)
            _context.Products.Remove(SelectedProduct);
            _context.SaveChanges();
        }
    }

}

CanUserAddRows

该属性起作用需要具备两个条件:

  1. DataGrid绑定数据源
  2. 绑定的数据项具有默认无参构造函数