我正在使用C#代碼將圖像添加到WPF堆棧面板。爲了定位圖像,我使用margin
關鍵字,但是當我運行該項目時,爲最後一個圖像創建了一個空白區域並推送第一個圖像。爲什麼? 在下面,我已經加載了來自同一個來源的不同邊距的兩幅圖像,並且您看到第一幅圖像被第二幅圖像的白色空間所覆蓋。請注意,源圖像是png
圖像,並且沒有任何邊框。在WPF中添加的圖像互相疊加
的代碼如下(請注意,我首先使用圖像控制,然後我使用的邊界控制和兩者都具有相同的問題):
Border newLegBorder =new Border();
BitmapImage casingLegBitmapImage = new BitmapImage(new Uri("images/casingleg.png", UriKind.Relative));
newLegBorder.Background = new ImageBrush(casingLegBitmapImage);
newLegBorder.Width = casingLegBitmapImage.Width;
newLegBorder.Height = casingLegBitmapImage.Height;
newLegBorder.SetValue(Grid.ColumnProperty, 0);
newLegBorder.SetValue(Grid.RowProperty, 0);
newLegBorder.VerticalAlignment = VerticalAlignment.Top;
newLegBorder.Margin = new Thickness(0, 0, 100, 0);
newLegBorder.Width = casingLegBitmapImage.Width;
newLegBorder.Height = casingLegBitmapImage.Height;
schematic.Children.Add(newLegBorder);
Border newLeg2Border = new Border();
BitmapImage casingLeg2BitmapImage = new BitmapImage(new Uri("images/casingleg.png", UriKind.Relative));
newLeg2Border.Background = new ImageBrush(casingLeg2BitmapImage);
newLeg2Border.Width = casingLeg2BitmapImage.Width;
newLeg2Border.Height = casingLeg2BitmapImage.Height;
newLeg2Border.SetValue(Grid.ColumnProperty, 0);
newLeg2Border.SetValue(Grid.RowProperty, 0);
newLeg2Border.VerticalAlignment = VerticalAlignment.Top;
newLeg2Border.Margin = new Thickness(100, 0, 0, 0);
newLeg2Border.Width = casingLeg2BitmapImage.Width;
newLeg2Border.Height = casingLeg2BitmapImage.Height;
schematic.Children.Add(newLeg2Border);
<Window
xmlns="schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="schemas.microsoft.com/winfx/2006/xaml"
xmlns:telerikDocking="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Docking"
xmlns:System="clr-namespace:System;assembly=mscorlib"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
xmlns:Chromes="clr-namespace:Telerik.Windows.Controls.Chromes;assembly=Telerik.Windows.Controls"
xmlns:Primitives="clr-namespace:Telerik.Windows.Controls.Primitives;assembly=Telerik.Windows.Controls.Navigation"
x:Class="imagetoolbox.wellSchematic"
Title="wellSchematic"
Height="402" Width="458">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="236"/>
</Grid.ColumnDefinitions>
<StackPanel x:Name="schematic" HorizontalAlignment="Left" Height="371" VerticalAlignment="Top" Width="214">
</StackPanel>
</Grid>
</Window>
你期待它看起來像什麼? – plast1k
我是第二個plast1k的問題。 – Hrethric
圖像不能擱置。圖片必須與此圖像相同: – ali