在WPF中,DataGridView
是WinForms中的控件,WPF中对应的控件是DataGrid
。要实现将TextBox中的数据添加到DataGrid,我们需要理解以下几个关键概念:
<!-- MainWindow.xaml -->
<Window x:Class="WpfApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="DataGrid示例" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Orientation="Horizontal" Margin="10">
<TextBox x:Name="InputTextBox" Width="200" Margin="0,0,10,0"/>
<Button Content="添加" Click="AddButton_Click" Width="80"/>
</StackPanel>
<DataGrid x:Name="DataGridControl" Grid.Row="1" Margin="10" AutoGenerateColumns="True"/>
</Grid>
</Window>
// MainWindow.xaml.cs
using System.Windows;
using System.Collections.ObjectModel;
using System.ComponentModel;
public partial class MainWindow : Window
{
public ObservableCollection<Person> People { get; set; }
public MainWindow()
{
InitializeComponent();
People = new ObservableCollection<Person>();
DataGridControl.ItemsSource = People;
}
private void AddButton_Click(object sender, RoutedEventArgs e)
{
if (!string.IsNullOrWhiteSpace(InputTextBox.Text))
{
People.Add(new Person { Name = InputTextBox.Text });
InputTextBox.Clear();
}
}
}
public class Person : INotifyPropertyChanged
{
private string _name;
public string Name
{
get { return _name; }
set
{
_name = value;
OnPropertyChanged(nameof(Name));
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
<!-- MainWindow.xaml -->
<Window x:Class="WpfApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApp"
Title="MVVM DataGrid示例" Height="350" Width="525">
<Window.DataContext>
<local:MainViewModel/>
</Window.DataContext>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Orientation="Horizontal" Margin="10">
<TextBox Text="{Binding InputText, UpdateSourceTrigger=PropertyChanged}" Width="200" Margin="0,0,10,0"/>
<Button Command="{Binding AddCommand}" Content="添加" Width="80"/>
</StackPanel>
<DataGrid ItemsSource="{Binding People}" Grid.Row="1" Margin="10" AutoGenerateColumns="True"/>
</Grid>
</Window>
// MainViewModel.cs
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Input;
public class MainViewModel : INotifyPropertyChanged
{
private string _inputText;
public string InputText
{
get { return _inputText; }
set
{
_inputText = value;
OnPropertyChanged(nameof(InputText));
}
}
public ObservableCollection<Person> People { get; set; }
public ICommand AddCommand { get; set; }
public MainViewModel()
{
People = new ObservableCollection<Person>();
AddCommand = new RelayCommand(AddPerson);
}
private void AddPerson(object parameter)
{
if (!string.IsNullOrWhiteSpace(InputText))
{
People.Add(new Person { Name = InputText });
InputText = string.Empty;
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
// RelayCommand.cs
using System;
using System.Windows.Input;
public class RelayCommand : ICommand
{
private readonly Action<object> _execute;
private readonly Predicate<object> _canExecute;
public RelayCommand(Action<object> execute) : this(execute, null) { }
public RelayCommand(Action<object> execute, Predicate<object> canExecute)
{
_execute = execute ?? throw new ArgumentNullException(nameof(execute));
_canExecute = canExecute;
}
public bool CanExecute(object parameter) => _canExecute?.Invoke(parameter) ?? true;
public void Execute(object parameter) => _execute(parameter);
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
}
这种功能常见于:
以上实现提供了从简单到复杂的解决方案,可以根据项目需求选择合适的实现方式。
没有搜到相关的文章