窗口边距和窗口边距Window.Padding 不起作用

发布于 2024-08-16 19:28:37 字数 867 浏览 5 评论 0原文

我正在设置窗口的属性边距和填充,但它没有生效:

这是一个示例:

<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>

结果:
alt text

期望的结果是标签的红框应距窗口框架 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:
alt text

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 技术交流群。

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

发布评论

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

评论(2

烟燃烟灭 2024-08-23 19:28:37

边距不起作用并不奇怪,因为边距是控件周围放置的空间量。对于 Window,这意味着使框架更小(和偏移),而不是客户区域,这会有点奇怪(并且可能无法与 Win32 托管环境很好地配合,不确定)。填充不起作用有点令人惊讶,我不确定为什么会这样。

但是,有一种解决方法可以封装在样式中:将默认的 Window ControlTemplate 替换为您自己的、尊重 Padding 的模板:(

<ControlTemplate TargetType="Window">
  <Border Background="White" Padding="{TemplateBinding Padding}">
    <ContentPresenter />
  </Border>
</ControlTemplate>

您可能希望边框背景成为生产代码的动态窗口背景画笔,但您明白了。)

显然,您可以将此模板放入样式模板设置器中,以避免在每个窗口上重复它。

以下是完整的模板(使用 Microsoft Expression 生成):

<Style x:Key="WindowStyle" TargetType="{x:Type Window}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Window}">
                <Border Background="{TemplateBinding Background}"
                    BorderBrush="{TemplateBinding BorderBrush}"
                    BorderThickness="{TemplateBinding BorderThickness}"
                    Margin="{TemplateBinding Margin}"
                    Padding="{TemplateBinding Padding}">

                    <AdornerDecorator>
                        <ContentPresenter/>
                    </AdornerDecorator>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="ResizeMode" Value="CanResizeWithGrip">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Window}">
                        <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">

                            <Grid>
                                <AdornerDecorator>
                                    <ContentPresenter/>
                                </AdornerDecorator>
                                <ResizeGrip
                                    x:Name="WindowResizeGrip"
                                    HorizontalAlignment="Right"
                                    VerticalAlignment="Bottom"
                                    IsTabStop="false"
                                    Visibility="Collapsed"
                                />
                            </Grid>
                        </Border>
                        <ControlTemplate.Triggers>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition
                                        Property="ResizeMode"
                                        Value="CanResizeWithGrip"
                                    />
                                    <Condition 
                                        Property="WindowState"
                                        Value="Normal"
                                    />
                                </MultiTrigger.Conditions>
                                <Setter 
                                    Property="Visibility"
                                    TargetName="WindowResizeGrip"
                                    Value="Visible"/>
                            </MultiTrigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Trigger>
    </Style.Triggers>
</Style>

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:

<ControlTemplate TargetType="Window">
  <Border Background="White" Padding="{TemplateBinding Padding}">
    <ContentPresenter />
  </Border>
</ControlTemplate>

(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):

<Style x:Key="WindowStyle" TargetType="{x:Type Window}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Window}">
                <Border Background="{TemplateBinding Background}"
                    BorderBrush="{TemplateBinding BorderBrush}"
                    BorderThickness="{TemplateBinding BorderThickness}"
                    Margin="{TemplateBinding Margin}"
                    Padding="{TemplateBinding Padding}">

                    <AdornerDecorator>
                        <ContentPresenter/>
                    </AdornerDecorator>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="ResizeMode" Value="CanResizeWithGrip">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Window}">
                        <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">

                            <Grid>
                                <AdornerDecorator>
                                    <ContentPresenter/>
                                </AdornerDecorator>
                                <ResizeGrip
                                    x:Name="WindowResizeGrip"
                                    HorizontalAlignment="Right"
                                    VerticalAlignment="Bottom"
                                    IsTabStop="false"
                                    Visibility="Collapsed"
                                />
                            </Grid>
                        </Border>
                        <ControlTemplate.Triggers>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition
                                        Property="ResizeMode"
                                        Value="CanResizeWithGrip"
                                    />
                                    <Condition 
                                        Property="WindowState"
                                        Value="Normal"
                                    />
                                </MultiTrigger.Conditions>
                                <Setter 
                                    Property="Visibility"
                                    TargetName="WindowResizeGrip"
                                    Value="Visible"/>
                            </MultiTrigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Trigger>
    </Style.Triggers>
</Style>
○愚か者の日 2024-08-23 19:28:37

这是一个简单的替代方案:只需在 Window 上设置背景颜色,并在 Window 内的 Grid 上设置 Margin :

Here's a simple alternative: just set a background colour on your Window and the Margin on the Grid within your Window:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文