Reputation: 4601
I have a class "Seive", a ObservableCollection seiveData object.
public Seive(String id)
{
SeiveIdSize = id;
Caret = 0.0;
Percent = 0.0;
Peices = 0;
Weight = 0.0;
Size = 0;
}
public String SeiveIdSize { get; set; }
public Double Weight { get; set; }
public Double Percent { get; set; }
public Double Caret { get; set; }
public uint Size { get; set; }
public uint Peices { get; set; }
In my xml : I have
<DataGrid Name="serverGrid" ItemsSource="{Binding}" .....>
<DataGrid.Columns>
<DataGridTextColumn Header="SEIVE" Width="Auto" Binding="{Binding Path=SeiveIdSize}" SortDirection="Ascending" />
<DataGridTextColumn Header="CTS" Width="Auto" Binding="{Binding Path=Caret}" />
<DataGridTextColumn Header=" % " Width="Auto" Binding="{Binding Path=Percent}" />
<DataGridTextColumn Header="PCS" Width="Auto" Binding="{Binding Path=Peices}" />
<DataGridTextColumn Header="WGT" Width="Auto" Binding="{Binding Path=Weight}" />
<DataGridTextColumn Header="SIZE" Width="Auto" Binding="{Binding Path=Size}" />
</DataGrid.Columns>
In window Loaded event, I fill 2 seives in seiveData, yet I don't see any results/rows.
seivesData.Add(new Seive("+10"));
seivesData.Add(new Seive("+8"));
seivesDataGrid.DataContext = seivesData;
EDIT : Button Event Code :
private void saveBtn_Click(object sender, RoutedEventArgs e)
{
Seive s1 = new Seive("+2");
s1.Peices = 100;
s1.Caret = 0.41;
s1.Weight = 0.10;
seivesData.Add(s1);
Seive s = seivesData[0];
s.Caret = 0.54;
s.Weight = 0.32;
seivesData[0] = s;
seiveDG.DataContext = seivesData;
}
Where am I going wrong ? I can see the newly added Seive all details, but Not the Caret & Weight added to 0th seive.
Upvotes: 1
Views: 1010
Reputation: 5691
The basic problem i see in your xaml is that you set the ItemsSource = {binding SeiveData} which is wrong if your pass this property into data context of the grid.
This below code is working now. check it.
one more thing if your want to notify the chagnes into the class Seive then must implement the INotifyPropertyChange interface.
XAML
<DataGrid Name="serverGrid" ItemsSource="{Binding}">
<DataGrid.Columns>
<DataGridTextColumn Width="Auto"
Binding="{Binding Path=SeiveIdSize}"
Header="SEIVE"
SortDirection="Ascending" />
<DataGridTextColumn Width="Auto"
Binding="{Binding Path=Caret}"
Header="CTS" />
<DataGridTextColumn Width="Auto"
Binding="{Binding Path=Percent}"
Header=" % " />
<DataGridTextColumn Width="Auto"
Binding="{Binding Path=Peices}"
Header="PCS" />
<DataGridTextColumn Width="Auto"
Binding="{Binding Path=Weight}"
Header="WGT" />
<DataGridTextColumn Width="Auto"
Binding="{Binding Path=Size}"
Header="SIZE" />
</DataGrid.Columns>
</DataGrid>
Cose behind
ObservableCollection<Seive> _seiveData;
public ObservableCollection<Seive> SeiveData
{
get { return _seiveData; }
set { _seiveData = value; }
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
SeiveData = new ObservableCollection<Seive>();
SeiveData.Add(new Seive("+10"));
SeiveData.Add(new Seive("+8"));
serverGrid.DataContext = SeiveData;
}
class
public class Seive
{
public Seive(String id)
{
SeiveIdSize = id;
Caret = 0.0;
Percent = 0.0;
Peices = 0;
Weight = 0.0;
Size = 0;
}
public String SeiveIdSize { get; set; }
public Double Weight { get; set; }
public Double Percent { get; set; }
public Double Caret { get; set; }
public uint Size { get; set; }
public uint Peices { get; set; }
}
Upvotes: 2
Reputation: 48600
One way as every one stated is to bind it to ObservableCollection
.
Another way is to bind from DataTable.
Convert your List to DataTable using
How to: Implement CopyToDataTable Where the Generic Type T Is Not a DataRow
Creating a DataTable From a Query (LINQ to DataSet)
I found this today. Very Effective.
Upvotes: 0
Reputation: 3399
You mention you're using a list, but for databinding to work, you would need to use ObservableCollection<T>
which will notify the datagrid if the collection changes.
EDIT:
As pointed out in the comments, it's not about changes. You're already setting your datacontext to the list, so there won't be a seiveData
in your DataContext. Instead, try the following:
ItemsSource="{Binding}"
If you still need to notify your view of changes, consider ObservableCollection<T>
as has been mentioned.
Upvotes: 1
Reputation: 36815
Probably the DataContext of the container, the DataGrid is in, is not set to the object that serves the seiveData
-property. Or maybe your seiveData
-list is not even backed by a property but is only a field. This is not allowed. Even if the field (member variable) is declared as public, the binding engine will not bind to it - it must be backed by a property.
Pleasee notice also, if you fill the list in Loaded-event, seiveList
must be an INotifyCollectionChanged
-implementing collection such as ObservableCollection<T>
because the binding is done before the population of the list. Otherwise, the DataGrid will not known that new data has been inserted into the list.
Upvotes: 0