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