We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Limited capability in binding ObservableCollection to WPF SfDataGrid

I have not been able to define the SfDataGrid ItemSource in XAML because the ObservableCollection is not populated in the ViewModel before the Views XAML runs.  Use of XAML DynamicResource does not help.  I can work around this problem by defining the ItemSource in code because this allows for making this definition after the ObservableCollection is populated.  This issue points to another issue that I cannot work around.  That other issue is that I cannot see the content of the SfDataGrid change when I add or delete records in the ObservableCollection.  I am using the latest Syncfusion WPF (May 3, 2016) on VS 2015. 

I have none of these problems using the WPF DataGrid that comes standard.  With that I can add or delete row programmatically and view the changes in the View and it works even with XAML StaticResource.

I have attached a simple version of the code to demonstrate these points.  It show two data grids (SfDataGrid & standard DataGrid) and it has an "Add New" button that adds a record to the ObservableCollection.  The "Add New" button was put there to demonstrate how the view changes a record is added to ObservableCollection. 

Thank You,for your help,
John

Attachment: SimpleGrid_baa17022.zip

3 Replies

SV Srinivasan Vasu Syncfusion Team June 7, 2016 03:51 AM UTC

Hi John, 
 
Thanks for contacting Syncfusion support. 
 
We have checked your query and we are unable to run your sample which throws the “FodyWeaver task failed” error. We have prepared a sample as per your sample and checked the reported “ItemsSource not binding properly in SfDataGrid” issue in our sample. The SfDataGrid has been updated property while adding or deleting records programmatically.  
 
For your reference we have attached the tested sample. Please revert by modifying the attached sample based on your application along with the replication procedure. It will be helpful for us analyze further.  
 
 
Regards, 
Srinivasan 
 



JM john mullally June 9, 2016 05:25 AM UTC

Hi Srinivasan,

This example gets rid of the “FodyWeaver task failed” error.  It demonstrates the points I made in the opening post and it has no dependencies outside of VS 2015 & Syncfusion.


Thank you for the help,
John

Attachment: BindingExample_e4ab7c21.zip


SV Srinivasan Vasu Syncfusion Team June 10, 2016 05:57 PM UTC

Hi John, 
We have checked your query and While using SfDataGrid, you have bind the datasource to SfDataGrid.ItemsSource properly.  
 
Please refer the below code changes. 
 
XAML 
  <Syncfusion:SfDataGrid x:Name="SfDataGrid1" HorizontalAlignment="Left" Margin="0,50,0,0" DataContext="{DynamicResource ViewModelKey}" 
                               VerticalAlignment="Top" AutoGenerateColumns="False" ItemsSource="{Binding Favorites}"> 
            <Syncfusion:SfDataGrid.Columns> 
                <Syncfusion:GridTextColumn x:Name="nameColumn" MappingName="Name" HeaderText="Name"/> 
                <Syncfusion:GridTextColumn x:Name="colorColumn" MappingName="Color" HeaderText="Color"/> 
            </Syncfusion:SfDataGrid.Columns> 
        </Syncfusion:SfDataGrid> 
 
For your reference we have attached the tested video and ItemsSource binding properly in SfDataGrid. 
 
Video Link: ObservableCollection 
 
Regards, 
Srinivasan 
 




Loader.
Live Chat Icon For mobile
Up arrow icon