Question

I have a DataGrid in WPF which should show a collection of Books. Books look like this:

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;
    }
}

Here is the Writer class:

    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);
    }
}

Now I want to show the writers in a single cell of the DataGrid. Something like this:

Title      |     Writer
------------------------
Book1      |     Writer1
           |     Writer2
------------------------
Book2           Writer1
...

But in case I'm trying to bind the List to the cell it only shows "Collection" in the cell as value.

This is the XAML I'm using:

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

I've already tried to bind the cell to "Writers.Fullname" but that also doesn't work. Could you please help me, I couldn't find any examples of such a binding.

Greetings

SyLuS

Was it helpful?

Solution

You could create DataGridTemplateColumn column with ItemsControl which would bind to Writers list:

<ToolKit:DataGridTemplateColumn Header="Writer">
   <ToolKit:DataGridTemplateColumn.CellTemplate>
      <DataTemplate>
         <ItemsControl ItemsSource="{Binding Writers}" DisplayMemberPath="Fullname"/>
      </DataTemplate>
   </ToolKit:DataGridTemplateColumn.CellTemplate>
</ToolKit:DataGridTemplateColumn>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top