在 silverlight 中将图像居中的最简单方法?

发布于 2024-07-14 17:19:12 字数 445 浏览 5 评论 0原文

假设我有一个 3 行的网格。 在中心行中,我想放置一个图像,然后将其垂直和水平居中于该行。 但由于(我认为)Silverlight 在居中时使用元素的左上角,因此当我告诉它垂直/水平居中时,图像的左上角也居中。

我见过两种方法:

  • 修改事件中的图像位置
  • 将图像的所有边距设置为非常大的数字(500)

有没有更简单的方法? 看起来有点奇怪,你需要做一些类似于这两种方法的事情。 我更愿意告诉它它的锚点位于中心,然后告诉它居中。 嗯,可能是一个很好的自定义控件。

更新:真正令人讨厌的事情是当我对页面进行了微小的更改以更改图像的居中方式时,FF3 显示了旧版本的页面。 这让我觉得这不起作用。 然后我在IE中打开它,它看起来是正确的。 我刷新了 FF3 的缓存,它显示了正确的页面。 真他妈烦人。

Say I have a grid with 3 rows. In the center row I'd like to put an image and then center it in that row vertically and horizontally. But since (I think) Silverlight uses upper left corners of an element when it centers, the upper left corner of the image is centered when I tell it to center vertically/horizontally.

I've seen 2 approaches to this:

  • modify the image position in an event
  • set the image's margin to a very large number on all sides (500)

Is there an easier way? Seems kinda weird that you'd need to do something hacky like those 2 approaches. I'd prefer to just tell the thing that its anchor is in the center and then tell it to center. Hmmm, could be a good custom control.

Update: the really obnoxious thing about this was when I made a minor change to the page to change how that image was centered and FF3 displayed an old version of the page. Which made me think that it didn't work. Then I pulled it up in IE and it looked right. I flushed FF3's cache and it displayed the right page. Damn annoying.

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

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

发布评论

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

评论(1

与君绝 2024-07-21 17:19:12

为什么这对你不起作用?

<Grid x:Name="LayoutRoot" Background="White">
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
        <RowDefinition />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <Image Source="Butterfly.jpg"
           Height="50" Width="50"
           HorizontalAlignment="Center" VerticalAlignment="Center"
           Stretch="UniformToFill"
           Grid.Column="1" Grid.Row="1" />
</Grid>

Why wouldn't this work for you?

<Grid x:Name="LayoutRoot" Background="White">
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
        <RowDefinition />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <Image Source="Butterfly.jpg"
           Height="50" Width="50"
           HorizontalAlignment="Center" VerticalAlignment="Center"
           Stretch="UniformToFill"
           Grid.Column="1" Grid.Row="1" />
</Grid>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文