I am constructing a datagrid in my WPF application.ItemSource
of the Datagrid is bounded to an IEnumerable
collection. I donno the ViewModel of my project. When I bind my datagrid itemsource to the datagrid I get column headers and Row values on the fly.
I donno the headers. It might be anything. I need to display detailed information of the selected row in the datagrid in a grid.
To do this i need to bing SelectedItem.HeaderName to the textblock of the grid.
But what the issue here is I donno the name of the header. So i can't simply hardcode SelectedItem.Headername
.
And number of columns may differ respectively. So my detailed view should also dynamic number Header names with its respective value when a row of my datagrid is selected.
As of now i have harcoded and seen the result in my xaml like below. because for a particular file i know their respective column headers,
<Label HorizontalAlignment="Stretch"
VerticalAlignment="Center"
Grid.Row="0"
Grid.Column="0">Header2:
</Label>
<TextBlock Grid.Row="0"
Grid.Column="1"
Name="Header2"
Text="{Binding SelectedItem.date, ElementName=dataGrid1}"
Width="auto"
Height="auto"
HorizontalAlignment="Stretch"
VerticalAlignment="Center" />
<Label Grid.Row="0"
Grid.Column="2"
VerticalAlignment="Center">Header3:</Label>
<TextBlock Grid.Row="0"
Grid.Column="3"
Name="username"
Text="{Binding SelectedItem.Header3, ElementName=dataGrid1}"
Width="auto"
Height="auto"
HorizontalAlignment="Stretch"
VerticalAlignment="Center" />
<Label Grid.Row="0"
Grid.Column="4"
VerticalAlignment="Center">Header4:</Label>
<TextBlock Grid.Row="0"
Grid.Column="5"
Name="level"
Text="{Binding SelectedItem.header4, ElementName=dataGrid1}"
Width="auto"
Height="auto"
HorizontalAlignment="Stretch"
VerticalAlignment="Center" />
<Label Grid.Row="1"
Grid.Column="0"
VerticalAlignment="Center">Header5:</Label>
<TextBlock Grid.Row="1"
Grid.Column="1"
Name="logger"
Text="{Binding SelectedItem.header5, ElementName=dataGrid1}"
Width="auto"
Height="auto"
HorizontalAlignment="Stretch"
VerticalAlignment="Center" />
<Label Grid.Row="1"
Grid.Column="2"
VerticalAlignment="Center">Headr6:</Label>
<TextBlock Grid.Row="1"
Grid.Column="3"
Name="thread"
Text="{Binding SelectedItem.header6, ElementName=dataGrid1}"
Width="auto"
Height="auto"
HorizontalAlignment="Stretch"
VerticalAlignment="Center" />
Since i am a begginer i couldn't figure out how to do this. I will be glad if u try to help me. And suggest some concepts i need to read related to this dynamic column generation, Count, assigning dynamic column headers to other control in UI. Thanks in advance!