2016-02-07 57 views
1

我的WPF應用程序有兩個文本框和一個DataGrid。有一個名爲「添加」的按鈕。每次按下按鈕時,我都想將保存在文本框中的數據添加到DataGrid中。在我的代碼中,第一次只更新DataGrid。我如何解決這個問題。如何在每次按下WPF按鈕時向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

下面是關於如何做到這一點的例子:

  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; } 
        } 
    } 
    
  3. SCREEN SHOT

簡單WPF數據網格&數據綁定示例:

enter image description here

相關問題