Reputation: 59
I am new with WPF and I am trying to add a new to the data grid I created.
The rows I am adding should be added dynamically however I can't see the values of the data in in the data grid.
Here is the 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>
Here is my code:
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;
}
}
}
}
Upvotes: 0
Views: 945
Reputation: 22435
EDIT: some general information for "dynamic controls" in wpf/mvvm
if you go the MVVM style you do something like this.
viewmodel
//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: use Binding to set the ItemsSource for your Datagrid
<DataGrid ItemsSource="{Binding MySource}">
<DataGrid.Columns>
<DataGridTextColumn Header="Field"/>
<DataGridTextColumn Header="Value"/>
</DataGrid.Columns>
</DataGrid>
thats all. the new row will display as far as your command is invoked through a button or something else
Upvotes: 1
Reputation: 138
if you have data in "DataTable" that you are trying to assign to datagrid then you can use datagrid.Datasource porperty
If you have a list or an array.. then just use foreach loop and under that loop add you rows.
Upvotes: 1