向datagrid添加新行动态

时间:2015-10-06 10:47:04

标签: c# wpf datagrid

我是WPF的新手,我正在尝试为我创建的数据网格添加新内容。

我添加的行应该动态添加,但是我无法在数据网格中看到数据的值。

这是xaml:

<Window x:Class="ProtocolAnalyzer.createByProtocol"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="createByProtocol" Height="506" Width="384">
<Grid Margin="0,0,2,4">
    <DataGrid x:Name="dataGridTable" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Height="452" Width="245">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Field"/>
            <DataGridTextColumn  Header="Value"/>
        </DataGrid.Columns>
    </DataGrid>

</Grid>
</Window>

这是我的代码:

using System;
using System.Collections.Generic;
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.Shapes;

namespace ProtocolAnalyzer
{
    /// <summary>
    /// Interaction logic for createByProtocol.xaml
    /// </summary>
    public partial class createByProtocol : Window
    {
        private ProtocolData.ProtocolData.Protocols _runningProtocol;

        public class Data
        {
            public string Name { get; set; }
            public string Value { get; set; }
        }

        public createByProtocol(ProtocolData.ProtocolData.Protocols protocol)
        {
            InitializeComponent();
            _runningProtocol = protocol;
            buildTable();
        }

        private void buildTable()
        {
            switch (_runningProtocol)
            {
                case  ProtocolData.ProtocolData.Protocols.ZBM:
                    dataGridTable.Items.Add("");
                    dataGridTable.Items[0] = "FFF";

                break;
            }
        }
    }
} 

2 个答案:

答案 0 :(得分:1)

如果你有数据&#34; DataTable&#34;您尝试分配给datagrid然后您可以使用datagrid.Datasource porperty

如果您有列表或数组.. 然后只需使用foreach循环,然后在该循​​环下添加行。

答案 1 :(得分:1)

编辑:一些一般信息for "dynamic controls" in wpf/mvvm

如果你采用MVVM风格,你会做这样的事情。

视图模型

//your data 
public ObservableCollection<Customer> MySource {get;set;}

//Command to add a new row
public ICommand AddNewCustomerCommand {get{return _lazyAddCommand.Value;}}

private readonly Lazy<DelegateCommand> _lazyAddCommand;

//ctor
public MyViewmodel()
{
   MySource = new ObservableCollection<Customer>();
   _lazyAddCommand= new Lazy<DelegateCommand>(() => new DelegateCommand(AddNewCustomerCommandExecute, CanAddNewCustomerCommandExecute));
 }

 private bool CanAddNewCustomerCommandExecute()
 {
     return true;//your Conditions goes here
 }

 private void AddNewCustomerCommandExecute()
 {
     if (!CanAddNewCustomerCommandExecute())
            return;
      //Add new Customer
      MySource.Add(new Customer());
  }

view:使用Binding为Datagrid设置ItemsSource

<DataGrid ItemsSource="{Binding MySource}">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Field"/>
        <DataGridTextColumn  Header="Value"/>
    </DataGrid.Columns>
</DataGrid>
这就是全部。只要通过按钮或其他东西调用命令,新行就会显示