与繁荣的财产结合

时间:2014-05-12 20:44:06

标签: wpf

我需要显示专辑封面照片和标题。在数据上下文中我有Album对象,但我不知道如何显示CoverPhoto我需要将ImageBrush ImageSource绑定到CoverPhoto.ThumbnailSource但是如何?我不能使用DataContext =" {Bindind CoverPhoto}" ImageSource =" {Binding ThumbnailSource}"

WPF:

<DataTemplate x:Key="AlbumItem">
    <Grid Width="360" Height="240">
        <Grid.Background>
            <ImageBrush ImageSource="{Binding CoverPhoto, Converter={StaticResource ThumbnailConverter}, ConverterParameter=340}" Stretch="UniformToFill" AlignmentX="Center" AlignmentY="Center" />
        </Grid.Background>
        <Grid.RowDefinitions>
            <RowDefinition Height="3*" />
            <RowDefinition Height="1*" />
        </Grid.RowDefinitions>
        <StackPanel Grid.Row="1" Margin="12">
            <TextBlock Style="{StaticResource ItemHeaderText}" Text="{Binding Title, Converter={StaticResource TextPlainConverter}, ConverterParameter=140}" MaxHeight="40" />
            <TextBlock Style="{StaticResource ItemSubheaderText}" Text="{Binding StartTime, Converter={StaticResource TextPlainConverter}, ConverterParameter=280}" />
        </StackPanel>
    </Grid>
</DataTemplate>

C#:

class Album
{
    public string Title { get; set; }
    public string StartTime { get; set; }
    public Photo CoverPhoto { get; set; }
}

class Photo
{
    public string Title { get; set; }
    public string Description { get; set; }
    BitmapImage PhotoSource { get; }
    BitmapImage ThumbnailSource { get; }
}

1 个答案:

答案 0 :(得分:1)

您可以使用ImageSource="{Binding CoverPhoto.ThumbnailSource}"来避免更改DataContext

另请注意,您可能需要在INotifyPropertyChangedAlbum类中实施Photo。如果没有这个,后面的代码所做的更改将不会反映在前端。