【问题标题】:Image margin behavior图像边距行为
【发布时间】:2011-12-26 00:33:06
【问题描述】:

当我在运行时设置图像边距时,它的行为很奇怪,与通过 xaml 代码设置时不同。

我希望边距值为 (40, 16, 0, 0)。

当我在运行时添加它时,我的代码是这样的:

        System.Windows.Controls.Image proba = new System.Windows.Controls.Image();
        ImageSource imageSource = GetSlidePart(PresentationDocument.Open(path, false), 0, "rId2");
        proba.Source = imageSource;
        proba.Width = 2245721 / 9525;
        proba.Height = 2286004 / 9525;
        proba.Margin = new Thickness(40, 16, 0, 0);
        gridName.Children.Add(proba);

但是,当我在 xaml 中添加图像时,cs 中的代码是这样的:

        image1.Source = GetSlidePart(PresentationDocument.Open(path, false), 0, "rId2");

xaml 中的代码是这样的:

<Window x:Class="GetInfoFromPptxFile.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="720" Width="960">

<Grid Name="gridName">
    <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="416,20,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
    <Image Height="150" HorizontalAlignment="Left" Margin="40,16,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="200" />
</Grid>
</Window>

当我运行我的程序时,image1 正是我想要的位置,而名为 proba 的图像却不是。当我给他们相同的边距值时,为什么会这样?

【问题讨论】:

  • 我们需要的不止这些。你想要什么和你得到什么的图像。另外:proba.Width = 2245721 / 9525; 有什么魔力? XAML 版本中缺少它...
  • 源图是一样的。高度和宽度也不重要,我尝试设置相同的宽度和高度,问题仍然存在。

标签: c# wpf image margin


【解决方案1】:

那是因为您没有在 C# 中设置水平和垂直对齐方式。

尝试添加:

proba.VerticalAlignment = VerticalAlignment.Top;
proba.HorizontalAlignment = HorizontalAlignment.Left;

它看起来会一样。

【讨论】:

    猜你喜欢
    • 2015-04-14
    • 1970-01-01
    • 2018-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-03
    • 2014-08-22
    • 1970-01-01
    相关资源
    最近更新 更多