How to Bind columns in Datagrid

Bircan, Burakhan 0 Reputation points
2023-11-08T13:28:56.1666667+00:00

I want to bind a column dynamically into the datagrid but datagrid does not allow this. I can only add static columns as I share below.

 <DataGrid AutoGenerateColumns="False"
           CanUserAddRows="False"
           CanUserDeleteRows="False"
           CanUserReorderColumns="False"
           CanUserResizeColumns="False"
           CanUserResizeRows="False"
           RowHeaderWidth="0"
           BorderBrush="{DynamicResource TertiaryBackgroundColor}"
           Background="{DynamicResource PrimaryBackgroundColor}"
           CanUserSortColumns="False"
           ItemsSource="{Binding LPList}"
           ScrollViewer.CanContentScroll="False"
           IsReadOnly="True"
           Margin="7 0 7 10"
           ScrollViewer.VerticalScrollBarVisibility="Visible"
           ScrollViewer.HorizontalScrollBarVisibility="Visible"
           TextBlock.FontSize="12">
    <DataGrid.Columns>
  		<DataGridTemplateColumn Header="{lex:Loc Key=MeterDate}">       
			<DataGridTemplateColumn.CellStyle>           	
				<Style TargetType="DataGridCell">            
				</Style>       
			</DataGridTemplateColumn.CellStyle>       
			<DataGridTemplateColumn.CellTemplate>           
				<DataTemplate>               
					<ItemsControl ItemsSource="{Binding Path=MeterDate}" Margin="10 0 0 0">                   									  		 											<ItemsControl.ItemsPanel>                       
				<ItemsPanelTemplate>                           
					<StackPanel Orientation="Horizontal" />                       
				</ItemsPanelTemplate>                   
</ItemsControl.ItemsPanel>                   
						<ItemsControl.ItemTemplate>                       
							<DataTemplate>                           
								<Border Margin=" 1 1 1 1" CornerRadius="5">                              
									 <TextBlock Text="{Binding}" FontFamily="Fonts/#Saira SemiBold"	FontSize="12" Foreground="{DynamicResource PrimaryTextColor}" />                           
								</Border>                       
							</DataTemplate>                   
						</ItemsControl.ItemTemplate>               
					</ItemsControl>           
				</DataTemplate>       
			</DataGridTemplateColumn.CellTemplate>

	</DataGrid.Columns>
</DataGrid>
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,383 questions
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,673 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
765 questions
{count} votes