将项列表绑定到Datagrid单元格

时间:2014-05-09 09:41:43

标签: c# wpf binding datagrid

我在WPF中有一个DataGrid,它应该显示一系列Books。 书籍看起来像这样:

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
...

但是如果我试图将List绑定到单元格,它只会显示&#34; Collection&#34;在细胞中作为价值。

这是我正在使用的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>

我已经尝试将单元格绑定到&#34; Writers.Fullname&#34;但这也行不通。 能帮助我吗,我找不到任何这种约束的例子。

问候

SyLuS

1 个答案:

答案 0 :(得分:0)

您可以使用DataGridTemplateColumn创建ItemsControl列,该列将绑定到Writers列表:

<ToolKit:DataGridTemplateColumn Header="Writer">
   <ToolKit:DataGridTemplateColumn.CellTemplate>
      <DataTemplate>
         <ItemsControl ItemsSource="{Binding Writers}" DisplayMemberPath="Fullname"/>
      </DataTemplate>
   </ToolKit:DataGridTemplateColumn.CellTemplate>
</ToolKit:DataGridTemplateColumn>