我正在我的WPF应用程序中构建一个数据网格 . Datagrid的 ItemSource
被绑定到 IEnumerable
集合 . 我不知道我的项目的ViewModel . 当我将datagrid itemsource绑定到datagrid时,我会动态获取列 Headers 和Row值 .
我不知道 Headers . 它可能是任何东西 . 我需要在网格中显示数据网格中所选行的详细信息 .
为此,我需要将SelectedItem.HeaderName绑定到网格的文本块 . 但这里的问题是我不知道 Headers 的名称 . 所以我不能简单地硬编码 SelectedItem.Headername
.
列数可能分别不同 . 因此,当我选择一行数据网格时,我的详细视图也应该是动态数字 Headers 名称及其各自的值 .
截至目前,我已经在我的xaml中进行了编码并看到了如下结果 . 因为对于特定的文件,我知道它们各自的列 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" />
由于我是一个乞丐,我无法弄清楚如何做到这一点 . 如果你想帮助我,我会很高兴的 . 并建议我需要阅读的一些与此动态列生成相关的概念,Count,将动态列 Headers 分配给UI中的其他控件 . 提前致谢!
1 回答
我创建了一个包含动态生成网格(TextBlock TextBox clollection)的小项目,具体取决于DataGrid标头和在DataGrid中创建集合的对象属性 . 希望这就是你要找的东西 . 你可以从my SkyDrive下载它
XAML:
我在Loaded事件处理程序中设置了它:
文字课: