【发布时间】:2015-08-06 21:59:06
【问题描述】:
我正在尝试在运行时确定大小的控件中显示一些专辑
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>
后面的代码
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();
}
}
专辑类
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;
}
}
我可以看到所有带有名称、艺术家和封面的专辑,但封面大小不正确。
在 DataTemplate 之外的控件中,我可以检索 ThumbWidth 和 ThumbHeight
我错过了什么?
【问题讨论】: