I'm trying to display some albums in controls with size determined in runtime
XAML:
<Grid>
<Grid.Resources>
<DataTemplate x:Key="AlbumsTemplate">
<Grid>
<Border Width="{Binding ThumbWidth}" Height="{Binding ThumbHeight}">
<Border.Background>
<ImageBrush ImageSource="{Binding Cover}"/>
</Border.Background>
</Border>
<StackPanel>
<TextBlock Text="{Binding Artist}" />
<TextBlock Text="{Binding Name}" />
</StackPanel>
</Grid>
</DataTemplate>
</Grid.Resources>
<Grid.RowDefinitions>
<RowDefinition />
</Grid.RowDefinitions>
<ListBox ItemsSource="{Binding Albums}" ItemTemplate="{DynamicResource AlbumsTemplate}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
</Grid>
Code behind
public MainWindow()
{
InitializeComponent();
DataContext = new ViewModel();
}
ViewModel.cs
class ViewModel
{
private ObservableCollection<Album> Albums { get; set; }
public int ThumbWidth { get; set; }
public int ThumbHeight { get; set; }
public ViewModel()
{
ThumbWidth = 150;
ThumbHeight = 150;
Albums = DataSupplier.Instance.GetAlbums();
}
}
Album Class
class Album
{
public string Name { get; set; }
public string Artist { get; set; }
public BitmapImage Cover { get; set; }
public Album(string name, string artist, BitmapImage cover)
{
Name = name;
Artist = artist;
Cover = cover;
}
}
I can see all albums with name, artist, and cover, but the cover has no the correct size.
In controls outside the DataTemplate I can retrieve ThumbWidth and ThumbHeight
What am I missing?