每次在WPF中按下按钮时,如何将数据添加到DataGrid

时间:2016-02-07 17:40:44

标签: c# wpf xaml datagrid

我的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 个答案:

答案 0 :(得分:1)

以下是如何执行此操作的示例:

  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. CODE

    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. SCREEN SHOT
  3. 简单的WPF DataGrid&amp;数据绑定示例:

    enter image description here