窗口边距和窗口边距Window.Padding 不起作用
我正在设置窗口的属性边距和填充,但它没有生效:
这是一个示例:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
SizeToContent="WidthAndHeight"
ResizeMode="NoResize"
Padding="22"
Margin="22">
<Grid>
<Label
FontWeight="Bold"
FontSize="36"
BorderThickness="1"
BorderBrush="Red"
Content="Hello world!"/>
</Grid>
</Window>
结果:
期望的结果是标签的红框应距窗口框架 44px (边距+填充)。
是的,我知道我可以设置标签的边距,但这不是我想要的。 我有一个整个项目,它的所有窗口都设置为一种样式,我想在常规窗口样式中设置此属性(或其他属性)。
我想如果我找不到任何解决方案,我将创建一个贪婪的命名样式,在其中设置边距/填充,然后我将逐个窗口并设置网格的样式,但这是我想做的最后一个选项。< br> 提前致谢。
I am setting peroperty Margin and Padding of a window and it doesn't take effect:
Here is an example:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
SizeToContent="WidthAndHeight"
ResizeMode="NoResize"
Padding="22"
Margin="22">
<Grid>
<Label
FontWeight="Bold"
FontSize="36"
BorderThickness="1"
BorderBrush="Red"
Content="Hello world!"/>
</Grid>
</Window>
Result:
The desired result is that the red frame of the lable should be away 44px from the window's frame (margin+padding).
Yes, I know I can set the margin of the label, but that's not what I want.
I have a whole project that all its windows are set to a style, I want to set this properties (or other) in the general window style.
I guess if I won't find any solution I will create a named style for greed where I will set the margin/padding, then I will go Window by window and set the Grid's style, but that's the last option I wanna do.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
边距不起作用并不奇怪,因为边距是控件周围放置的空间量。对于 Window,这意味着使框架更小(和偏移),而不是客户区域,这会有点奇怪(并且可能无法与 Win32 托管环境很好地配合,不确定)。填充不起作用有点令人惊讶,我不确定为什么会这样。
但是,有一种解决方法可以封装在样式中:将默认的 Window ControlTemplate 替换为您自己的、尊重 Padding 的模板:(
您可能希望边框背景成为生产代码的动态窗口背景画笔,但您明白了。)
显然,您可以将此模板放入样式模板设置器中,以避免在每个窗口上重复它。
以下是完整的模板(使用 Microsoft Expression 生成):
It's not surprising that Margin doesn't work, because Margin is the amount of space to be placed around the control. For a Window, this would mean making the frame smaller (and offset), not the client area, and that would be a bit strange (and might not play nicely with the Win32 hosting environment, not sure). It is a bit surprising that Padding doesn't work, and I'm not sure why that would be.
However, there is a workaround which you can encapsulate in a style: replace the default Window ControlTemplate with your own template that does respect the Padding:
(You would probably want the Border Background to be the dynamic window background brush for production code, but you get the idea.)
Obviously you can put this template in a style Template setter so as to avoid having to repeat it on each Window.
Here is the full template (generated with Microsoft Expression):
这是一个简单的替代方案:只需在
Window
上设置背景颜色,并在Window
内的Grid
上设置Margin
:Here's a simple alternative: just set a background colour on your
Window
and theMargin
on theGrid
within yourWindow
: