繁体   English   中英

如何在WPF中每次按下按钮时都向DataGrid添加数据

[英]How to add data to the DataGrid in every time when button pressed in WPF

我的WPF应用程序有两个文本框和一个DataGrid。 有一个名为“添加”的按钮。 我想在每次按下按钮时将保存在文本框中的数据添加到DataGrid中。 在我的代码中,第一次仅更新DataGrid。 我该如何解决。

XAML

  <TextBox x:Name="txt_description" Margin="0,10,20,15"/>
  <TextBox x:Name="txt_quantity" Margin="0,10,20,15" PreviewKeyDown="txt_quantity_PreviewKeyDown"/>
  <Button x:Name="btn_Add" Margin="0,0,0,15" Content="Add" FontSize="18" HorizontalAlignment="Left" VerticalAlignment="Bottom" Width="100" Click="btn_Add_Click" />
<DataGrid x:Name="dgd_items" Margin="10,10,10,10" FontSize="16"/>

C#

 private void btn_Add_Click(object sender, RoutedEventArgs e)
    {
        ItemsDto obj = new ItemsDto();
        obj.ItemCode = cmb_ItemCode.SelectedItem.ToString();
        obj.descripition = txt_description.Text;
        obj.qty = Convert.ToInt32(txt_quantity.Text);

        Dto.Add(obj);

        dgd_items.ItemsSource = Dto;

    }

这是有关如何执行此操作的示例:

  1. XAML

     <Window x:Class="SolutionDatagrid.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"></ColumnDefinition> <ColumnDefinition Width="300"></ColumnDefinition> </Grid.ColumnDefinitions> <DataGrid x:Name="dgCustomers" ItemsSource="{Binding Customers}" Grid.Column="0"></DataGrid> <StackPanel Grid.Column="1" Orientation="Vertical" Margin="10"> <TextBlock Margin="10" FontWeight="Bold">Customer Information:</TextBlock> <StackPanel Orientation="Vertical" Margin="10"> <TextBlock>First Name</TextBlock> <TextBox x:Name="txtFirstName"/> </StackPanel> <StackPanel Orientation="Vertical" Margin="10"> <TextBlock>Last Name</TextBlock> <TextBox x:Name="txtLastName"/> </StackPanel> <StackPanel Orientation="Vertical" Margin="10"> <TextBlock>Favourite Color</TextBlock> <ComboBox x:Name="cbxColor" ItemsSource="{Binding Colours}"/> </StackPanel> <TextBlock Margin="10" FontWeight="Bold">Actions:</TextBlock> <Button Margin="10" Height="50" Click="Button_Add_Click">Add Data</Button> <Button Margin="10" Height="50" Click="Button_Reset_Click">Reset</Button> </StackPanel> </Grid> </Window> 
  2.  using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace SolutionDatagrid { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { this.InitializeComponent(); // List of Customers this.Customers = new ObservableCollection<Customer>(); // List of Colours this.Colours = new List<string> { "Red", "Orange", "Yellow", "Green", "Blue", "Indigo", "Violet" }; this.DataContext = this; } // Properties public List<string> Colours { get; set; } public ObservableCollection<Customer> Customers { get; set; } // Button Click Events private void Button_Add_Click(object sender, RoutedEventArgs e) { // Variables to hold customer data string firstName = txtFirstName.Text; string lastName = txtLastName.Text; string colour = cbxColor.SelectedItem.ToString(); // If our data is valid if(firstName !=null && lastName!=null && colour!=null) { // Create new customer using data Customer customer = new Customer { Name = firstName, Surname = lastName, Colour = colour }; // Add the new cutomer to the existing list of customers this.Customers.Add(customer); } } private void Button_Reset_Click(object sender, RoutedEventArgs e) { this.Customers.Clear(); } } // Customer Class public class Customer { // Properties public string Name { get; set; } public string Surname { get; set; } public string Colour { get; set; } } } 
    1. 屏幕拍摄

简单的WPF DataGrid和数据绑定示例:

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM