在单击DataGrid中的按钮后,我正try 在C#WPF MVVM中添加另一行.问题是,我一点击其他地方(例如,在文本框中),该行就会被添加.这意味着在我单击"添加行"按钮后,数据网格不会立即更新.
NewDocumentView.xaml
<DataGrid x:Name="DataGrid"
AutoGenerateColumns="False"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Grid.Row="1"
Margin="20,350,0,0"
ItemsSource="{Binding Items}">
<DataGrid.Columns>
<DataGridTextColumn Header="Description" Binding="{Binding Description}" Width="500"/>
<DataGridTextColumn Header="Unit" Binding="{Binding Unit}" Width="100"/>
<DataGridTextColumn Header="Price" Binding="{Binding Price}" Width="100"/>
<DataGridTextColumn Header="Total" Binding="{Binding Total}" Width="100"/>
<DataGridTemplateColumn Header="Actions">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="Add Row" Command="{Binding AddItemCommand}" Width="100"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
NewDocumentViewModel.cs
namespace App.MVVM.ViewModel
{
internal class NewDocumentViewModel : ObservableObject
{
public ObservableCollection<Item> Items { get; set; } = new ObservableCollection<Item>();
public RelayCommand AddItemCommand { get; set; }
public NewDocumentViewModel()
{
Items = new ObservableCollection<Item>();
AddItemCommand = new RelayCommand(o => { AddNewItem(); });
}
private void AddNewItem()
{
Items.Add(new Item());
}
}
}
NewDocumentView.xaml.cs个
namespace App.MVVM.View
{
public partial class NewDocumentView : UserControl
{
public NewDocumentView()
{
InitializeComponent();
DataContext = new NewDocumentViewModel();
}
}
}
Item.cs个
namespace App.MVVM.Model
{
class Item : ObservableObject
{
private string _description;
public string Description
{
get { return _description; }
set
{
if (_description != value)
{
_description = value;
OnPropertyChanged(nameof(Description));
}
}
}
private string _unit;
public string Unit
{
get { return _unit; }
set
{
if (_unit != value)
{
_unit = value;
OnPropertyChanged(nameof(Unit));
}
}
}
private int _price;
public int Price
{
get { return _price; }
set
{
if (_price != value)
{
_price = value;
OnPropertyChanged(nameof(Price));
}
}
}
private int _total;
public int Total
{
get { return _total; }
set
{
if (_total != value)
{
_total = value;
OnPropertyChanged(nameof(Total));
}
}
}
}
}
ObservableObject.cs
namespace App.Core
{
internal class ObservableObject : INotifyPropertyChanged
{
public event PropertyChangedEventHandler? PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
RelayCommand.cs个
namespace App.Core
{
internal class RelayCommand : ICommand
{
private readonly Action<object> execute;
private readonly Func<object, bool> canExecute;
public RelayCommand(Action<object> execute, Func<object, bool> canExecute = null)
{
this.execute = execute;
this.canExecute = canExecute;
}
public event EventHandler CanExecuteChanged
{
add => CommandManager.RequerySuggested += value;
remove => CommandManager.RequerySuggested -= value;
}
public bool CanExecute(object parameter)
{
return canExecute == null || canExecute(parameter);
}
public void Execute(object parameter)
{
execute(parameter);
}
}
}
我哪里做错了?如何使用按钮将新行添加到数据网格以立即更新数据网格?