2014-09-25 153 views
0

我想创建一个窗口,它将显示一个在另一个之下的图片列表。我创建了一个包含视框控制和图像在它:WPF Viewbox和图像大小

<UserControl x:Class="..." 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300"> 
    <Grid> 
     <Viewbox Name="viewbox"> 
      <Image Height="10" Name="image" Width="10" HorizontalAlignment="Left" VerticalAlignment="Top" /> 
     </Viewbox> 
    </Grid> 
</UserControl> 

public BitmapImage Image 
{ 
    get { return image.Source as BitmapImage; } 
    set { changeImage(value); } 
} 
public SingleIllustrationViewer() 
{ 
    InitializeComponent(); 
} 
private void changeImage(BitmapImage img) 
{ 
    System.Drawing.Graphics graphics = System.Drawing.Graphics.FromHwnd(IntPtr.Zero); 
    float dpiX = graphics.DpiX/96; 
    this.image.BeginInit(); 
    this.image.Source = img; 
    this.image.EndInit(); 
    this.image.Width = img.PixelWidth/img.DpiX * dpiX; 
} 

,我把图片上的窗口是这样的:

double margin = 0; 
for (int i = 0; i < illustrations.Count; i++) 
{ 
    String path = illustrations[i].printVersions.Last<String>(); 
    BitmapImage bmp = new BitmapImage(new Uri(path)); 

    Controls.SingleIllustrationViewer iv = new Controls.SingleIllustrationViewer(); 
    iv.VerticalAlignment = System.Windows.VerticalAlignment.Top; 
    iv.HorizontalAlignment = System.Windows.HorizontalAlignment.Left; 
    iv.Margin = new Thickness(50, margin, 0, 0); 
    iv.Image = bmp; 
    grid.Children.Add(iv); 
    margin += iv.Image.Height + 20; 
} 

因此,举例来说,我已经放在拍摄3张照片(所有3个相同的宽度)就像这样,并且得到了这样一个有趣的行为:第一个是好的,第二个是小的,第三个小于一秒。这里是屏幕截图: scree shot

也许有人可以告诉我为什么是这样,以及如何解决这个问题,以同样的宽度看到所有这些图片?

谢谢!

的问候,托马斯

+0

你尝试视框的Stretch属性:http://msdn.microsoft.com/en-us/library/system.windows.controls.viewbox.stretch(v=vs.110).aspx – bit 2014-09-25 08:33:18

+0

您增加了每幅图片的边距:'margin + = iv.Image.Height + 20;' – Bolu 2014-09-25 11:37:09

回答

0

根源:

没有指定高度或用户控件的宽度,所以当第一SingleIllustrationViewer添加到Grid,它会被拉伸到占据所有可用空间直到它到达Grid的边缘。第二个发生同样的情况,但由于margin增加,所以限制在较小的区域。

指定为

d:DesignHeight="300" d:DesignWidth="300" 

大小仅用于设计,设置大小像

Height="300" Width="300" 

,然后把你的观众在StackPanel而不是Grid,那么你不”不得不根据最后观众的位置来计算观看者的边缘。 StackPanel是一个容器,它可以在一个方向上垂直或水平地堆叠其子项。

for (int i = 0; i < illustrations.Count; i++) 
{ 
    String path = illustrations[i].printVersions.Last<String>(); 
    BitmapImage bmp = new BitmapImage(new Uri(path)); 

    Controls.SingleIllustrationViewer iv = new Controls.SingleIllustrationViewer(); 
    iv.VerticalAlignment = System.Windows.VerticalAlignment.Top; 
    iv.HorizontalAlignment = System.Windows.HorizontalAlignment.Left; 
    iv.Margin = new Thickness(50, 0, 0, 20); //50 px to the left, 20 px to the next child 
    iv.Image = bmp; 
    stackPanel1.Children.Add(iv); 
} 
+0

嗨,谢谢你的回答。但如果我不增加保证金图片被放置在另一个。 – Tom1410 2014-09-25 12:07:04

+0

把你的观众放在一个'StackPanel'而不是'Grid'中,那么你就不必在最后一个观众的位置上计算观众的边距。 StackPanel是一个容器,它可以在一个方向上垂直或水平地堆叠子项。 – kennyzx 2014-09-25 12:26:05

+0

我试过了,但现在我只看到了我的第一张图片。 – Tom1410 2014-09-25 12:57:07