这是UserControl的构造函数,其中包含DataGrid元素。
public Results(List<CartItem> Items)
{
InitializeComponent();
this.Items.DataContext = Items;
}
这是DataGrid的XAML。
<DataGrid AutoGenerateColumns="False" Name="Items" IsReadOnly="True" MinHeight="300">
<DataGrid.Columns>
<DataGridCheckBoxColumn>
<DataGridCheckBoxColumn.HeaderTemplate>
<DataTemplate>
<CheckBox Style="{StaticResource styleCheckBox}" IsChecked="{Binding IsSelected}"/>
</DataTemplate>
</DataGridCheckBoxColumn.HeaderTemplate>
</DataGridCheckBoxColumn>
<DataGridTextColumn Header="Abone No" Binding="{Binding SubscriberNo}" />
<DataGridTextColumn Header="Adı Soyadı" Binding="{Binding SubscriberName}" />
<DataGridTextColumn Header="Fatura Tutarı" Binding="{Binding _Amount, Mode=OneWay}" />
<DataGridTextColumn Header="Son Ödeme Tarihi" Binding="{Binding _Deadline, Mode=OneWay}" />
</DataGrid.Columns>
</DataGrid>
我仔细检查了DataGrid的DataContext对象是否已正确填充。但DataGrid中没有显示任何数据。我做错了什么?
答案 0 :(得分:2)
您只设置了Grid的DataContext。您还必须像这样设置Datagrid的ItemsSource:
<DataGrid ItemsSource="{Binding}"