我是一个能够帮助翻译文本的助手。
以下是需要翻译的内容:
我在WPF中有一个DataGrid,应该显示一系列书籍。书籍长这样:
public class Book
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
private string title = "";
public string Title
{
get
{
return title;
}
set
{
title = value;
OnPropertyChanged("Title");
}
}
private List<Writer> writers = new List<Writer>();
public List<Writer> Writers
{
get
{
return writers;
}
set
{
writers = value;
OnPropertyChanged("Writers");
}
}
public Book() { }
public Book(string bookTitle)
{
this.Title = bookTitle;
}
}
这是Writer类:
public class Writer
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
private string fullname = "";
public string Fullname
{
get
{
return fullname;
}
set
{
fullname = value;
OnPropertyChanged("Fullname");
}
}
private string forename = "";
public string Forename
{
get
{
return forename;
}
set
{
forename = value;
OnPropertyChanged("Forename");
}
}
private string surename = "";
public string Surename
{
get
{
return surename;
}
set
{
surename = value;
OnPropertyChanged("Surename");
}
}
public Writer() { }
public Writer(string writersForename, string writersSurename)
{
this.Forename = writersForename;
this.Surename = writersSurename;
this.Fullname = string.Format("{0} {1}", this.Forename, this.Surename);
}
}
现在我想在DataGrid的单元格中显示作者。就像这样:
Title | Writer
------------------------
Book1 | Writer1
| Writer2
------------------------
Book2 Writer1
...
但是如果我尝试将列表绑定到单元格,它只在单元格中显示“Collection”作为值。
这是我使用的XAML:
<ToolKit:DataGrid x:Name="dataGrid1" DataContext="{StaticResource ItemCollectionViewSource}" IsReadOnly="True" ItemsSource="{Binding}" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" AutoGenerateColumns="False" >
<ToolKit:DataGrid.Columns>
<ToolKit:DataGridTextColumn Header="Title" Binding="{Binding Path=Title}" />
<ToolKit:DataGridTextColumn Header="Writer" Binding="{Binding Path=Writers}" />
</ToolKit:DataGrid.Columns>
</ToolKit:DataGrid>
我已经尝试将单元格绑定到 "Writers.Fullname",但也不起作用。 你能帮我吗?我找不到任何这样绑定的例子。
问候
SyLuS