How to bind the list of string to WPF DataGrid?
WPF DataGrid (SfDataGrid) is bound to a collection with a class type. You can bind a list of string as an ItemsSource of SfDataGrid can be achieved by using GridTemplateColumn.
public class ViewModel { public ViewModel() { this.DataFieldList = new List<string>() { "Item1", "Item2" , "Item3", "Item4", "Item5", "Item6", "Item7", "Item8", "Item9", "Item10" }; } public List<string> DataFieldList { get; set; } }
<Window.DataContext> <local:ViewModel /> </Window.DataContext> <Grid> <syncfusion:SfDataGrid Name="dataGrid" AllowFiltering="False" AllowResizingColumns="False" AutoGenerateColumns="False" ColumnSizer="Star" NavigationMode="Row" ItemsSource="{Binding DataFieldList, UpdateSourceTrigger=PropertyChanged}"> <syncfusion:SfDataGrid.Columns> <syncfusion:GridTemplateColumn HeaderText="Column Name" > <syncfusion:GridTemplateColumn.CellTemplate> <DataTemplate> <TextBlock Text="{Binding}" /> </DataTemplate> </syncfusion:GridTemplateColumn.CellTemplate> </syncfusion:GridTemplateColumn> </syncfusion:SfDataGrid.Columns> </syncfusion:SfDataGrid> </Grid>
View WPF DataGrid Binding Demo in GitHub
Conclusion
I hope you enjoyed learning about how to bind the list of string to WPF DataGrid.
You can refer to our WPF Grid feature tour page to know about its other groundbreaking feature representations. You can also explore our WPF Grid documentation and WPF Grid example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!