我想創建一個窗口,它將顯示一個在另一個之下的圖片列表。我創建了一個包含視框控制和圖像在它: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個相同的寬度)就像這樣,並且得到了這樣一個有趣的行爲:第一個是好的,第二個是小的,第三個小於一秒。這裏是屏幕截圖:
也許有人可以告訴我爲什麼是這樣,以及如何解決這個問題,以同樣的寬度看到所有這些圖片?
謝謝!
的問候,托馬斯
你嘗試視框的Stretch屬性:http://msdn.microsoft.com/en-us/library/system.windows.controls.viewbox.stretch(v=vs.110).aspx – bit 2014-09-25 08:33:18
您增加了每幅圖片的邊距:'margin + = iv.Image.Height + 20;' – Bolu 2014-09-25 11:37:09