如何在DataGrid myDataGrid的Sections字段下显示List<string> Sections
的{{1}}项。
Combobox
目前,我使用以下代码填充myDataGrid中的其余字段。
List<string> Sections = new List<string> {"C50", "C40", "C30"};
答案 0 :(得分:1)
如果您使用AutoGenerateColumns="True"
,则应使用AutoGeneratingColumn
事件(msdn)。
XAML:
<DataGrid x:Name="myDataGrid" ItemsSource="{Binding}"
AutoGenerateColumns="True"
AutoGeneratingColumn="myDataGrid_AutoGeneratingColumn"/>
代码隐藏:
private void myDataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
if (e.PropertyName == "Sections")
{
var cb = new DataGridComboBoxColumn();
cb.Header = "Sections";
cb.ItemsSource = new List<string> { "C50", "C40", "C30" };
cb.SelectedValueBinding = new Binding("Sections");
e.Column = cb;
}
}
AutoGenerateColumns="False"
的解决方案:
XAML:
<DataGrid x:Name="myDataGrid" ItemsSource="{Binding}"
AutoGenerateColumns="False"
>
<DataGrid.Columns>
<DataGridTextColumn Header="Id" Binding="{Binding Id}" />
<DataGridTextColumn Header="X" Binding="{Binding X}" />
<DataGridTextColumn Header="Y" Binding="{Binding Y}" />
<DataGridTextColumn Header="StartElevation" Binding="{Binding StartElevation}" />
<DataGridTextColumn Header="EndElevation" Binding="{Binding EndElevation}" />
<DataGridTextColumn Header="FoundationHeight" Binding="{Binding FoundationHeight}" />
<DataGridComboBoxColumn x:Name="cbSections" Header="Sections" SelectedItemBinding="{Binding Sections}"/>
</DataGrid.Columns>
</DataGrid>
代码隐藏:
...
{
...
myDataGrid.ItemsSource = data;
cbSections.ItemsSource = new List<string> { "C50", "C40", "C30" };
...
}