Custom Window

public class CustomWnd : System.Windows.Window
    {
        static CustomWnd()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomWnd), new FrameworkPropertyMetadata(typeof(CustomWnd)));
        }

        public CustomWnd()
        {
            CommandBindings.Add(new CommandBinding(SystemCommands.CloseWindowCommand, CloseWindow));
            CommandBindings.Add(new CommandBinding(SystemCommands.MaximizeWindowCommand, MaximizeWindow, CanResizeWindow));
            CommandBindings.Add(new CommandBinding(SystemCommands.MinimizeWindowCommand, MinimizeWindow));
            CommandBindings.Add(new CommandBinding(SystemCommands.RestoreWindowCommand, RestoreWindow, CanResizeWindow));
        }

        private void CanResizeWindow(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = ResizeMode == ResizeMode.CanResize || ResizeMode == ResizeMode.CanResizeWithGrip;
        }

        private void RestoreWindow(object sender, ExecutedRoutedEventArgs e)
        {
            this.WindowState = System.Windows.WindowState.Normal;
        }

        private void MinimizeWindow(object sender, ExecutedRoutedEventArgs e)
        {
            this.WindowState = System.Windows.WindowState.Minimized;
        }

        private void MaximizeWindow(object sender, ExecutedRoutedEventArgs e)
        {
            this.WindowState = System.Windows.WindowState.Maximized;
        }

        private void CloseWindow(object sender, ExecutedRoutedEventArgs e)
        {
            this.Close();
        }
    }

<Style TargetType="{x:Type wnd:CustomWnd}" BasedOn="{StaticResource ResourceKey={x:Type Window}}">
        <Setter Property="WindowStyle" Value="SingleBorderWindow"/>
        <Setter Property="AllowsTransparency" Value="False"/>
        <Setter Property="BorderBrush" Value="Green"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="UseLayoutRounding" Value="True"/>
        <Setter Property="WindowChrome.WindowChrome">
            <Setter.Value>
                <WindowChrome CornerRadius="0" GlassFrameThickness="1" UseAeroCaptionButtons="False" NonClientFrameEdges="None" />
            </Setter.Value>
        </Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type wnd:CustomWnd}">
                    <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
                        <Grid x:Name="LayoutRoot" Background="{TemplateBinding Background}">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="*" />
                            </Grid.RowDefinitions>

                            <Grid x:Name="PART_WindowTitleGrid" Grid.Row="0" Height="30" Background="{TemplateBinding BorderBrush}">
                                <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="10,0">
                                    <Image Source="{TemplateBinding Icon}" Margin="0,0,5,0"/>
                                    <TextBlock Text="{TemplateBinding Title}" Foreground="{TemplateBinding Foreground}" VerticalAlignment="Center"/>
                                </StackPanel>

                                <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="5,0">
                                    <Button Width="24" Height="24" IsTabStop="False" WindowChrome.IsHitTestVisibleInChrome="True" Style="{StaticResource SysBtnStyle}" Command="{x:Static SystemCommands.MinimizeWindowCommand}">
                                        <Button.Content>
                                            <Path Stroke="White" StrokeThickness="2" Fill="Transparent" Data="M0,0 L10,0" Margin="0,5,0,0" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                                        </Button.Content>
                                    </Button>
                                    <Button x:Name="PART_MAX" Width="24" Height="24" IsTabStop="False" WindowChrome.IsHitTestVisibleInChrome="True" Style="{StaticResource SysBtnStyle}"/>
                                    <Button Width="24" Height="24" IsTabStop="False" WindowChrome.IsHitTestVisibleInChrome="True" Style="{StaticResource SysCloseBtnStyle}" Command="{x:Static SystemCommands.CloseWindowCommand}">
                                        <Button.Content>
                                            <Path Stroke="White" StrokeThickness="2" Fill="Transparent" Data="M0,0 L7.07,7.07 M0,7.07 L7.07,0" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                                        </Button.Content>
                                    </Button>
                                </StackPanel>
                            </Grid>

                            <AdornerDecorator Grid.Row="1" KeyboardNavigation.IsTabStop="False">
                                <ContentPresenter x:Name="MainContentPresenter" KeyboardNavigation.TabNavigation="Cycle" />
                            </AdornerDecorator>

                            <ResizeGrip x:Name="ResizeGrip" HorizontalAlignment="Right" VerticalAlignment="Bottom" Grid.Row="1"
                                            IsTabStop="False" Visibility="Hidden" WindowChrome.ResizeGripDirection="BottomRight" />
                        </Grid>
                    </Border>

                    <ControlTemplate.Triggers>
                        <Trigger Property="WindowState" Value="Maximized">
                            <Setter TargetName="PART_MAX" Property="Command" Value="{x:Static SystemCommands.RestoreWindowCommand}"/>
                            <Setter TargetName="PART_MAX" Property="Content">
                                <Setter.Value>
                                    <Path Stroke="White" StrokeThickness="2" Fill="Transparent" Data="M0,3 L5,3 L5,8 L0,8Z M3,3 L3,0 L8,0 L8,5 L5,5" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                        
                        <Trigger Property="WindowState" Value="Normal">
                            <Setter TargetName="PART_MAX" Property="Command" Value="{x:Static SystemCommands.MaximizeWindowCommand}"/>
                            <Setter TargetName="PART_MAX" Property="Content">
                                <Setter.Value>
                                    <Path Stroke="White" StrokeThickness="2" Fill="Transparent" Data="M0,0 L7.07,0 L7.07,7.07 L0,7.07Z" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                        
                        <Trigger Property="IsActive" Value="False">
                            <Setter Property="BorderBrush" Value="#FF6F7785" />
                        </Trigger>
                        
                        <Trigger Property="ResizeMode" Value="NoResize">
                            <Setter TargetName="PART_MAX" 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>
            </Setter.Value>
        </Setter>
    </Style>


 
原文地址:https://www.cnblogs.com/RR-ghost/p/9809929.html