18

ウィンドウのperopertyMarginと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離れている必要があることです(マージン+パディング)。

はい、ラベルの余白を設定できることはわかっていますが、それは私が望んでいることではありません。すべてのウィンドウがスタイルに設定されているプロジェクト全体があります。このプロパティ(またはその他)を一般的なウィンドウスタイルに設定したいと思います。

解決策が見つからない場合は、マージン/パディングを設定する貪欲の名前付きスタイルを作成し、ウィンドウごとに移動してグリッドのスタイルを設定しますが、これが最後のオプションです。
前もって感謝します。

4

2 に答える 2

11

マージンはコントロールの周囲に配置されるスペースの量であるため、マージンが機能しないのは当然のことです。ウィンドウの場合、これはクライアント領域ではなくフレームを小さくする(そしてオフセットする)ことを意味し、それは少し奇妙です(そして、Win32ホスティング環境ではうまく機能しないかもしれません、確かではありません)。パディングが機能しないのは少し驚きですが、なぜそうなるのかわかりません。

ただし、スタイルにカプセル化できる回避策があります。デフォルトのWindow ControlTemplateを、パディングを尊重する独自のテンプレートに置き換えます。

<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>
于 2010-01-03T02:27:21.997 に答える
7

Window簡単な代替方法は次のとおりです。あなたとあなたMarginGrid内の背景色を設定するだけですWindow

于 2013-09-06T00:06:41.053 に答える