图像边距行为

发布于 2024-12-14 09:57:29 字数 1408 浏览 5 评论 0原文

当我在运行时设置图像边距时,它的行为很奇怪,与我通过 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 的图像则不是。当我给他们相同的保证金值时,为什么会出现这种情况?

When I set my image margin in run-time it acts strange, different then when I set it via xaml code.

I want the margin value to be (40, 16, 0, 0).

When I add it in run-time, my code is this:

        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);

But, when I add image in xaml, the code in cs is this:

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

and the code in xaml is this:

<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>

When I run my program, image1 is exactly where I want it to be, and the image named proba is not. Why is this when I give them the same margin values?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

提笔落墨 2024-12-21 09:57:29

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

尝试添加:

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

看起来会一样。

That's because you're not setting the horizontal and vertical alignments in C#.

Try adding:

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

and it'll look the same.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文