0

I've searched high and low for a solution to my problem but I haven't been able to find anything that works. I'm at a complete loss at this point.

I have a custom class that has a BitmapImage property and a String property. Lets call this class "BusinessCard". I also have:

ObservableCollection< BusinessCard[] > CompanyCards = new ObservableCollection< BusinessCard[] >();

Each BusinessCard[] array holds an image of each employees business card as well as their name (The aforementioned BitmapImage and String properties)

What I want to accomplish is to have each row representing each company and each column representing a business card.

Example: I have Company A, Company B, and Company C. Lets say I have three cards for A, four for B, and two for C. I want my output to look like the following (Excluding the "Row #" part):

Row [Card] | [Card] | [Card]
 1   Name  |  Name  |  Name
----------------------------------
Row [Card] | [Card] | [Card] | [Card]
 2   Name  |  Name  |  Name  |  Name
----------------------------------
Row [Card] | [Card]
 3   Name  |  Name

BusinessCard class

public class BusinessCard: INotifyPropertyChanged
{
    private BitmapImage _image;
    private string _personName;
public BusinessCard() { }
public BusinessCard(BitmapImage image, string personName)
    {
        this.CardImage = image;
        this.Name = personName;
    }
public BitmapImage CardImage 
    {
    get { return _image; }
    set
        {
            _image = value;
            NotifyPropertyChanged();
        }
    }
public string Name
    {
    get { return _personName; }
    set
        {
            _personName = value;
        }
    }

public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

What I've Tried

XAML

This worked but of course only worked for a single BusinessCard from each company being displayed on each line (Correct number of rows but not columns):

    <DataGrid x:Name="CardViewer" AutoGenerateColumns="False" CanUserResizeColumns="False" CanUserResizeRows="False" CanUserSortColumns="False" CanUserAddRows="False" Grid.Column="0" ItemsSource="{Binding CompanyCards}">
        <DataGrid.Columns>
                <DataGridTemplateColumn Header="Cards" Width="*">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Vertical" HorizontalAlignment="Center">
                                <Image Source="{Binding CardImage}"/>
                                <Label Content="{Binding Name}"/>
                            </StackPanel>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>

I've also tried this but this created the rows but there was apparently no content in them as they were ~10 pixels high and I couldn't resize them even with the UserResize options being set to true:

XAML

<DataGrid x:Name="CardViewer" AutoGenerateColumns="False" CanUserResizeColumns="True" CanUserResizeRows="True" CanUserSortColumns="False" CanUserAddRows="False" Grid.Column="0"/>

Code Behind

    public ObservableCollection<DataGridRow> spList = new ObservableCollection<DataGridRow>();

    foreach(BusinessCard[] bca in CompanyCards)
        {
            StackPanel sp = new StackPanel();
            sp.Orientation = Orientation.Horizontal;
            sp.MinHeight = 32;
            DataGridRow dgr = new DataGridRow();
        foreach(BusinessCard bcb in bca)
            {
                Image img = new Image();
                img.Stretch = Stretch.Uniform;
                img.Source = bcb.CardImage;

                Label lbl = new Label();
                lbl.Content = rcb.Name;

                sp.Children.Add(img);
                sp.Children.Add(lbl);
            }
            dgr.Item = sp;
            spList.Add(dgr);
        }

        CardViewer.DataContext = spList;
        CardViewer.ItemsSource = spList;

I think I tried other variations of the two but all had the same result. Please help? I feel like there's something very simple that I'm missing...

HDL_CinC_Dragon
  • 194
  • 1
  • 8
  • I am not great at xaml but I suspect you are using the wrong collection, it appears that you need to group your cards by company and then cards either using linq to group your data or by company class that has a business card collection – Gavin Oct 26 '14 at 10:29

1 Answers1

1

Since each column in your grid represents an array, you should change the template of the cell to use an ItemsControl:

   <DataGrid x:Name="CardViewer" AutoGenerateColumns="False" CanUserResizeColumns="False" 
          CanUserResizeRows="False" CanUserSortColumns="False" CanUserAddRows="False" Grid.Column="0" 
          ItemsSource="{Binding}">
    <DataGrid.Columns>
        <DataGridTemplateColumn Header="Cards" Width="*">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <ItemsControl ItemsSource="{Binding}" >
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                   <StackPanel Orientation="Vertical" HorizontalAlignment="Center">
                        <Image Source="{Binding CardImage}" Height="50" Width="50"/>
                        <Label Content="{Binding Name}"/>
                    </StackPanel> 
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                        <ItemsControl.ItemsPanel>
                            <ItemsPanelTemplate>
                                <VirtualizingStackPanel Orientation="Horizontal"/>
                            </ItemsPanelTemplate>
                        </ItemsControl.ItemsPanel>
                    </ItemsControl>

                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>
user1064519
  • 2,180
  • 12
  • 13
  • Thanks for the reply! I'll give this a shot when I get home from work today and let you know/accredit accordingly! – HDL_CinC_Dragon Oct 27 '14 at 19:40
  • This worked brilliantly! Thank you so much! One note though, I found that setting the TemplateColumn's width to "*" versus "Auto" causes the column the be very scrunched up whereas "Auto" properly expands when filled with content. – HDL_CinC_Dragon Oct 28 '14 at 00:24