wpf 调整 XAML Grid.Background 图像的大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20749245/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adjust the size of a XAML Grid.Background Image
提问by nocarrier
I have a simple piece of XAML that lays out a group of tiles, each with a background image icon.
我有一个简单的 XAML 片段,它布置了一组图块,每个图块都有一个背景图像图标。
<Grid.Background>
<ImageBrush Stretch="None"
ImageSource="{Binding ImageSource}"
AlignmentY="Center"
AlignmentX="Center"/>
</Grid.Background>
This works fine, except the background image fills the square perfectly. I'm sure XAML thinks this is fine, but actually it needs to be about half the size to match spec.
这工作正常,除了背景图像完美地填充了正方形。我确信 XAML 认为这很好,但实际上它需要大约一半的大小才能匹配规范。
I tried several things including adding a border to the grid (which truncates the background image, keeping the dimensions but cutting it off on the sides and top) and adding margins and paddings in various places.
我尝试了几种方法,包括向网格添加边框(它会截断背景图像,保持尺寸但在侧面和顶部将其切断)以及在不同位置添加边距和填充。
I also played with making this a normal but everything floats over top of this background image so that is not an option.
我还尝试将其设为正常,但所有内容都漂浮在此背景图像的顶部,因此这不是一个选项。
Finally I should mention this is a Windows 8 Windows Store app using Windows Runtime, so certain features in WPF aren't available to me here (like ViewPort).
最后,我应该提到这是一个使用 Windows 运行时的 Windows 8 Windows 应用商店应用程序,因此 WPF 中的某些功能在这里对我不可用(例如 ViewPort)。
Question: how do you adjust the size/dimensions of a XAML grid background image?
问题:如何调整 XAML 网格背景图像的大小/尺寸?
Sidenote: I think the solution may lie in a transform... ?
旁注:我认为解决方案可能在于转换......?
Update: per poster request here is what the expected result should be. Even though it appears no float over the background image takes place here, in other places text floats over the background. That is why it needs to be a and not an element.
更新:这里的每个海报请求都是预期的结果。尽管这里看起来没有在背景图像上浮动,但在其他地方,文本会浮动在背景上。这就是为什么它需要是一个而不是一个元素。
How it looks now:
现在的样子:


How it should look:
它应该是什么样子:


回答by BradleyDotNET
A scale render transfrom should do what you want: (You may need to set CenterX and CenterY to get it to look exactly the way you want)
缩放渲染转换应该做你想做的:(你可能需要设置 CenterX 和 CenterY 才能让它看起来完全符合你的要求)
<Grid.Background>
<ImageBrush Stretch="None"
ImageSource="{Binding ImageSource}"
AlignmentY="Center"
AlignmentX="Center">
<ImageBrush.Transform>
<ScaleTransform ScaleX=".5" ScaleY=".5"/>
</ImageBrush.Transform>
</ImageBrush>
</Grid.Background>
回答by Erti-Chris Eelmaa
You can squeeze the ImageBrush into your custom ViewPort so that it would take the size you want it to.
您可以将 ImageBrush 挤压到您的自定义 ViewPort 中,以便它采用您想要的大小。
There's some information: http://keyvan.io/viewbox-and-viewport-in-windows-presentation-foundation
有一些信息:http: //keyvan.io/viewbox-and-viewport-in-windows-presentation-foundation
If you want to cut it half, then your start x & y should be 0.25 and end x,y 0.75. Something like this should do trick:
如果你想把它减半,那么你的开始 x & y 应该是 0.25,结束 x,y 应该是 0.75。这样的事情应该做的伎俩:
<Grid.Background>
<ImageBrush Stretch="None"
ImageSource="{Binding ImageSource}"
AlignmentY="Center"
AlignmentX="Center"
Viewport="0.25, 0.25, 0.75, 0.75"/>
</Grid.Background>
//Edit for Win8 maybe:
//为Win8编辑也许:
<Grid>
<Grid>
<Grid.RenderTransform>
<ScaleTransform RenderTransformOrigin="0.5, 0.5" ScaleX="0.5" ScaleY="0.5" />
</Grid.RenderTransform>
<Grid.Background>
<ImageBrush Stretch="None"
ImageSource="{Binding ImageSource}"
AlignmentY="Center"
AlignmentX="Center"/>
</Grid.Background>
</Grid>
</Grid>

