您好我想将此对象的绑定列表添加到datagrid:
class DataGridRows
{
public int Number{get; set;}
public int Avg {get; set;}
public List<Item> Items{get; set;}
}
class Item
{
public int Number{get; set;}
public int Avg {get; set;}
}
而XAML是:
<DataGrid ItemsSource="{Binding DataGridRows, UpdateSourceTrigger=PropertyChanged}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Avg}" FontSize="10"/>
<DataGridTextColumn Binding="{Binding Number}" FontSize="10"/>
<DataGridTextColumn Binding="{Binding Items.Avg}" Header="{Binding Items.Number}"></DataGridTextColumn>
</DataGrid.Columns>
这不正确。不要显示内部列表中的数据。
答案 0 :(得分:0)
<DataGrid ItemsSource="{Binding Items, UpdateSourceTrigger=PropertyChanged}" AutoGenerateColumns="False">
提供清单&lt;&gt;项目作为ItemsSource。由于列表包含Item的对象,因此可以将Item类的属性绑定到datagrid。
C#
class DataGridRows
{
public List<Item> Items{get; set;}
}
class Item
{
public int Number{get; set;}
public int Avg {get; set;}
}
XAML
<DataGridTemplateColumn Width="230" Header="Header Text" IsReadOnly="True" CanUserResize="False" >
<DataGridTemplateColumn.CellTemplate >
<DataTemplate>
<TextBlock Text="{Binding Number}" Foreground="White" HorizontalAlignment="Left" TextAlignment="Left" Margin="3,0,0,0" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Width="230" Header="Header Text" IsReadOnly="True" CanUserResize="False" >
<DataGridTemplateColumn.CellTemplate >
<DataTemplate>
<TextBlock Text="{Binding Avg}" Foreground="White" HorizontalAlignment="Left" TextAlignment="Left" Margin="3,0,0,0" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
您可以轻松使用这样的DataGridTemplate。