【问题标题】:Auto Set WP Image controls height and width based on the databound SourceAuto Set WP Image 根据数据绑定的 Source 控制高度和宽度
【发布时间】:2013-04-11 22:30:12
【问题描述】:

我有一个 WP 应用程序,我想在其中显示一个图像列表,这些图像数据绑定到从 Web 服务填充的 observablecollection。我希望图像列表以与检索它们的位置相同的高度和宽度显示。那么,如何让图像控件根据数据绑定源自动调整其高度和宽度。

使用以下代码,图像不显示。

  <Grid x:Name="LayoutRoot">
    <Image Source="{Binding AlbumPicture}" Stretch="Fill" Width="Auto" Height="Auto"/>
  </Grid>

以下工作,但每个图像的高度和宽度都不同。所以,它们看起来不太好。

  <Grid x:Name="LayoutRoot">
    <Image Source="{Binding AlbumPicture}" Stretch="Fill" Width="200" Height="120"/>
  </Grid>

我尝试了以下方法,但它们没有奏效。

  • 设置 Stretch="None"

  • Databind Image 控件的 Height、Width 属性到那些 使用以下图像,高度和宽度始终为0。

     BitmapImage bmp = new BitmapImage(new Uri(updatedAlbum.AlbumPicture, UriKind.RelativeOrAbsolute));
     album.AlbumHeight = bmp.PixelHeight;
     album.AlbumWidth = bmp.PixelWidth;
    

【问题讨论】:

    标签: image windows-phone-7 data-binding


    【解决方案1】:

    此代码仅在图像加载后才起作用。

     album.AlbumHeight = bmp.PixelHeight;
     album.AlbumWidth = bmp.PixelWidth;
    

    您只需等待,然后将此值绑定(或直接设置)到您的图像控件。

    例如你可以使用方法

    <Image x:Name="img" ImageOpened="Img_OnImageOpened"/>
    
    private void Img_OnImageOpened(object sender, RoutedEventArgs e)
    {
        //start from here to set width and height.
        //you can do it directly, or by the Binding
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-09
      • 1970-01-01
      • 1970-01-01
      • 2021-03-08
      相关资源
      最近更新 更多