1

I have created a border-less window template so that all window look the same. The template code looks like this:

public abstract class WindowBase : Window
{
    static WindowBase()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(WindowBase), new FrameworkPropertyMetadata(typeof(WindowBase)));
    }
}

And I have a style which looks like this:

    <Style TargetType="{x:Type WindowBase}">
        <!--<Setter Property="Topmost" Value="True" />-->
        <Setter Property="WindowStyle" Value="None" />
        <Setter Property="Background" Value="White"/>
        <Setter Property="ResizeMode" Value="CanResizeWithGrip" />
        <Setter Property="WindowState" Value="Maximized" />
        <Setter Property="AllowsTransparency" Value="True"/>
        <Setter Property="SnapsToDevicePixels" Value="true"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Window}">
                <Border Margin="0" BorderThickness="1">
                    <Grid>
                        <Grid Background="White" Visibility="Visible">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="1.5*"/>
                                <RowDefinition Height="10*"/>
                            </Grid.RowDefinitions>
                            <ContentPresenter Grid.Row="0" Grid.RowSpan="2"/>
                        </Grid>
                        <ContentPresenter />
                        <ResizeGrip Name="ResizeGroup" VerticalAlignment="Bottom" HorizontalAlignment="Right" KeyboardNavigation.IsTabStop="False"/>
                    </Grid>
                </Border>
                <ControlTemplate.Triggers>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="ResizeMode" Value="CanResizeWithGrip"/>
                            <Condition Property="WindowState" Value="Maximized"/>
                        </MultiTrigger.Conditions>
                        <Setter Property="Visibility" TargetName="ResizeGroup" Value="Collapsed"/>
                    </MultiTrigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Now when the application runs in maximized state, what I have observed is the window exceeds the screen space. Assume I am in 1600 x 900, my maximum work area would be 1600 x 900, but the application takes something like this: 1614 x 914. Also, using WPF Inspector, what I observed is the Top and Left position is in negative. It shows up as '-7' in my case.

I am not sure, where I am going wrong? Can someone help?

4

2 回答 2

1

ResizeMode设置为NoResize而不是CanResizeWithGrip. 这将删除调整大小的边框,这显然是在计算大小时考虑在内的,尽管不可见。

<Style TargetType="{x:Type WindowBase}">
    ...
    <Setter Property="ResizeMode" Value="NoResize" /> 
    ...
</Style>
于 2012-10-14T21:58:20.967 回答
0

尝试 :

    <ControlTemplate.Triggers>
                    <Trigger Property="IsActive" Value="True">
                        <Setter Property="BorderBrush" Value="{DynamicResource WindowBorderActive}" />
                    </Trigger>
                    <Trigger Property="WindowState" Value="Maximized">
                        <Setter TargetName="Maximize" Property="Visibility" Value="Collapsed" />
                        <Setter TargetName="Restore" Property="Visibility" Value="Visible" />
                        <Setter TargetName="LayoutRoot" Property="Margin" Value="7" />
                    </Trigger>
                    <Trigger Property="WindowState" Value="Normal">
                        <Setter TargetName="Maximize" Property="Visibility" Value="Visible" />
                        <Setter TargetName="Restore" Property="Visibility" Value="Collapsed" />
                    </Trigger>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="ResizeMode" Value="CanResizeWithGrip" />
                            <Condition Property="WindowState" Value="Normal" />
                        </MultiTrigger.Conditions>
                        <Setter TargetName="ResizeGrip" Property="Visibility" Value="Visible" />
                    </MultiTrigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>

Margin=7 可以解决问题。:)

于 2016-07-27T16:48:40.880 回答