加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何将网格中的图像控制居中?

发布时间:2020-12-15 22:06:41 所属栏目:百科 来源:网络整理
导读:有一个Grid,我将一个 Image控件放入Grid中. 我的工作:只需将属性Horizo??ntalAlignment和VerticalAlignment更改为’Center’. 然而,与其他控件不同,图像控制执行奇怪.这个图像控制中心本身按照其左上角如下: 我想知道为什么它以这种方式表现? 编辑 这是我
有一个Grid,我将一个 Image控件放入Grid中.
我的工作:只需将属性Horizo??ntalAlignment和VerticalAlignment更改为’Center’.

然而,与其他控件不同,图像控制执行奇怪.这个图像控制中心本身按照其左上角如下:

我想知道为什么它以这种方式表现?

编辑
这是我的XAML:

<UserControl x:Class="Entity.WPF.Controls.ShopProfile"
         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="600" d:DesignWidth="780">
<Grid>

    <DockPanel >
        <Grid>

            <Image  HorizontalAlignment="Center" Height="100" Margin="0" VerticalAlignment="Center" Width="100"/>
        </Grid>
    </DockPanel>
</Grid>

如果我设置边距如Margin =“ – 50,-50,0”,它实际上是居中的,但为什么其他控件不需要这个设置?

解决方法

这很有意思,我不确定为什么会发生这种情况,或者它是否记录在某处.

要回答您的问题,如何将图像控件置于网格中心,只需删除这些属性,图像将自动居中在网格中.

<Grid>
    <Image Height="100" Margin="0" Width="100" />
</Grid>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读