【发布时间】:2017-06-02 13:09:30
【问题描述】:
我知道在 WPF 中图像可以适合网格。现在网格也有可能适应图像吗?
我想使用自动布局将按钮放置在图像上的某些位置,但图像保持其原始形状也很重要(因此 Stretch=Uniform)。但是,当我首先使用网格的行和列时,按钮就在我想要的位置,但是如果我在宽度上而不是在高度上拉伸我的窗口,按钮就会偏离我想要的位置(基于图像)。
我想做什么很清楚吗?有谁知道我如何实现这一目标?
编辑: 在我的图像上,假设一个圆圈,我希望一个按钮始终靠近圆圈。 好的,这是我到目前为止的代码:
<Window x:Class="VakPumpEntwurf2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid Background="#FFE62626">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="69*" />
<ColumnDefinition Width="21*"/>
<ColumnDefinition Width="254*" />
<ColumnDefinition Width="173*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="55*" />
<RowDefinition Height="19*"/>
<RowDefinition Height="33*"/>
<RowDefinition Height="107*" />
<RowDefinition Height="107*" />
</Grid.RowDefinitions>
<Image Source="images/MyPic.png" Grid.ColumnSpan="4" Grid.RowSpan="5" >
</Image>
<Button Grid.Column="1" Grid.Row="1"/> //This Button shall be always near the circle
</Grid>
这里还有两张图片,希望能更清楚地说明我的问题到底出在哪里:
这是我拉伸窗口宽度后按钮所在的位置。但实际上我希望我的按钮总是非常靠近圆圈。
【问题讨论】:
-
你能给我们一些你的代码(这个问题所必需的部分)吗? SO 不是代码请求网站 ;-) 你有更大的机会通过展示你自己制作的东西来帮助你。
-
您可以将整个 UI 放入
ViewBox并将Stretch设置为Uniform。 -
如果你想在按钮和圆圈之间保持相同的距离,你应该考虑使用画布面板而不是网格。 Canvas 定义了一个区域,您可以在其中显式使用相对于 Canvas 区域的坐标定位子元素:msdn.microsoft.com/en-us/library/….
-
@mm8 值得一试,但我还能将 Canvas 的 backgroundimage 设置为 Stretch=Uniform 吗?
-
当然。看我的回答。
标签: c# wpf visual-studio