我试图从包含其中2个其他列表的列表中绑定属性,但由于某种原因,它不会在我的数据网格上显示任何内容。
正如您在下面看到的,我创建了一个observableCollection,当我调试时,该列表具有所有必需的属性(我试图在XAML绑定部分中添加的属性),但由于某种原因它不起作用。
初始化数据网格的类
private CategoryServiceClient quizClient = new CategoryServiceClient();
private IList<Category> quizList;
public QuizWindow()
{
InitializeComponent();
LoadUserData();
}
private void LoadUserData()
{
// The GetCategoriesQuestionAnswers method has a list of categories with a
// list of questions and a list of answers
quizList = new ObservableCollection<Category>(quizClient.GetCategoriesQuestionsAnswers());
try
{
quizDatagrid.ItemsSource = quizList;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
throw;
}
}
XAML
<DataGrid x:Name="quizDatagrid" CanUserAddRows="False" Margin="10,90,0,10" ClipboardCopyMode="None" AutoGenerateColumns="False" HorizontalAlignment="Left" Width="698">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding id}" Header="Category ID" />
<DataGridTextColumn Binding="{Binding name}" Header="Category Name" />
<DataGridTextColumn Binding="{Binding category.question.id}" Header="Question ID" />
<DataGridTextColumn Binding="{Binding category.question.description}" Header="Question Description" />
<DataGridTextColumn Binding="{Binding category.question.answer.id}" Header="Answer ID" />
<DataGridTextColumn Binding="{Binding category.question.answer.description}" Header="Answer Description" />
<DataGridTextColumn Binding="{Binding category.question.answer.isCorrect}" Header="Is Correct" />
<DataGridTextColumn Binding="{Binding category.question.answer.pointAmount}" Header="Point Amount" />
</DataGrid.Columns>
</DataGrid>
这是类别类
public class Category
{
[DataMember]
public int id { get; set; }
[DataMember]
public string name { get; set; }
[DataMember]
public int amount { get; set; }
[DataMember]
public Quiz quiz { get; set; }
[DataMember]
public List<Question> question { get; set; }
public Category()
{
question = new List<Question>();
}
}
这是我的问题类
[DataContract(IsReference = true)]
public class Question
{
[DataMember]
public int id { get; set; }
[DataMember]
public string description { get; set; }
[DataMember]
public Category category { get; set; }
[DataMember]
public bool isAnswered { get; set; }
[DataMember]
public List<Answer> Answers { get; set; }
public Question()
{
Answers = new List<Answer>();
}
}
答案 0 :(得分:0)
DataGrid只会为其ItemsSource中的每个项添加一行(在本例中为ObservableCollection),即每个 Category 对象获取一行,而不是每个Question对象中的一行一个类别。
它不是等级的。如果要在同一个DataGrid中显示属于特定类别的所有问题,可以使用包含嵌套DataGrid的DataGridTemplateColumn:
<DataGrid x:Name="quizDatagrid" CanUserAddRows="False" Margin="10,90,0,10"
ClipboardCopyMode="None" AutoGenerateColumns="False" HorizontalAlignment="Left" Width="698">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding id}" Header="Category ID" />
<DataGridTextColumn Binding="{Binding name}" Header="Category Name" />
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<DataGrid ItemsSource="{Binding question}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding id}" Header="Question ID" />
<DataGridTextColumn Binding="{Binding description}" Header="Question Description" />
<DataGridTextColumn Binding="{Binding answer.id}" Header="Answer ID" />
<DataGridTextColumn Binding="{Binding answer.description}" Header="Answer Description" />
<DataGridTextColumn Binding="{Binding answer.isCorrect}" Header="Is Correct" />
<DataGridTextColumn Binding="{Binding answer.pointAmount}" Header="Point Amount" />
</DataGrid.Columns>
</DataGrid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
您可以通过绑定到每个Category对象中问题列表中的第一个索引,在同一个顶级DataGrid中显示类别的第一个问题:
<DataGrid x:Name="quizDatagrid" CanUserAddRows="False" Margin="10,90,0,10" ClipboardCopyMode="None" AutoGenerateColumns="False" HorizontalAlignment="Left" Width="698">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding id}" Header="Category ID" />
<DataGridTextColumn Binding="{Binding name}" Header="Category Name" />
<DataGridTextColumn Binding="{Binding question[0].id}" Header="Question ID" />
<DataGridTextColumn Binding="{Binding question[0].description}" Header="Question Description" />
<DataGridTextColumn Binding="{Binding question[0].answer.id}" Header="Answer ID" />
<DataGridTextColumn Binding="{Binding question[0].answer.description}" Header="Answer Description" />
<DataGridTextColumn Binding="{Binding question[0].answer.isCorrect}" Header="Is Correct" />
<DataGridTextColumn Binding="{Binding question[0].answer.pointAmount}" Header="Point Amount" />
</DataGrid.Columns>
</DataGrid>
编辑:您将DataGrid的ItemsSource设置为ObservableCollection。每个类别都有一系列问题。每个问题都有一系列答案。 DataGrid将仅为每个类别呈现一行。您只能在此级别上显示单个问题和单个答案:
<DataGridTextColumn Binding="{Binding question[0].id}" Header="Question ID" />
<DataGridTextColumn Binding="{Binding question[0].description}" Header="Question Description" />
<DataGridTextColumn Binding="{Binding question[0].Answers[0].id}" Header="Answer ID" />
<DataGridTextColumn Binding="{Binding question[0].Answers[0].description}" Header="Answer Description