2014-09-25 68 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