WPF主页面布局
<Window x:Class="GenericTools.Views.MainView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:GenericTools" mc:Ignorable="d" WindowStartupLocation="CenterScreen" Background="Transparent" FontFamily="Microsoft YaHei" FontSize="12" FontWeight="ExtraLight" Title="MES" Height="700" Width="1150"> <WindowChrome.WindowChrome> <WindowChrome GlassFrameThickness="-1"/> </WindowChrome.WindowChrome> <Grid> <Grid.Background> <RadialGradientBrush> <GradientStop Color="#FF285173" Offset="0"/> <GradientStop Color="#FF244967" Offset="0.3"/> <GradientStop Color="#FF14273A" Offset="1"/> </RadialGradientBrush> </Grid.Background> <Grid.RowDefinitions> <RowDefinition Height="45"/> <RowDefinition /> <RowDefinition Height="20"/> </Grid.RowDefinitions> <Border BorderBrush="#5518AABD" BorderThickness="0,0,0,1"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="60"/> <ColumnDefinition Width="auto"/> <ColumnDefinition/> </Grid.ColumnDefinitions> <Image Source="../Asserts/Images/logo.png" Margin="0,7"/> <StackPanel Grid.Column="1" VerticalAlignment="Center"> <TextBlock Text="{Binding Title,RelativeSource={RelativeSource AncestorType=Window}}" Foreground="White" FontSize="16"/> <TextBlock Text="升职加薪 只争朝夕 以梦为马 不负韶华" Foreground="LightGray" FontSize="9" Margin="0,2,0,0"/> </StackPanel> <Grid Grid.Column="2"> <Grid.RowDefinitions> <RowDefinition/> <RowDefinition Height="13"/> </Grid.RowDefinitions> <Border> <Border.Style> <Style TargetType="Border"> <Style.Setters> <Setter Property="HorizontalAlignment" Value="Right"/> <Setter Property="WindowChrome.IsHitTestVisibleInChrome" Value="True"/> <Setter Property="Background" Value="Transparent"/> <Setter Property="Width" Value="200"/> </Style.Setters> <Style.Triggers> <DataTrigger Binding="{Binding WindowState, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}" Value="Maximized"> <Setter Property="Margin" Value="0,5,5,0"/> </DataTrigger> </Style.Triggers> </Style> </Border.Style> <!--遮挡住自带的三个按钮--> <StackPanel Orientation="Horizontal" HorizontalAlignment="Right"> <Button Content="" Style="{StaticResource MinButtonStyle}" Click="MinClick"/> <Button Style="{StaticResource ControlButtonStyle}" Click="MaxNormalClick"/> <Button Content="" Style="{StaticResource CloseButtonStyle}" Background="DarkRed" Click="CloseClick"/> </StackPanel> </Border> <Border Grid.Row="1" BorderBrush="#5518AABD" BorderThickness="0,1,0,0"/> <Border Grid.Row="1" Margin="0,3,0,2"> <Border.Background> <VisualBrush TileMode="Tile" Viewport="0,0,7,7" ViewportUnits="Absolute"> <VisualBrush.Visual> <Grid Width="20" Height="20"> <Line X1="0" Y1="10" X2="10" Y2="0" Stroke="Gray" StrokeThickness="1"/> </Grid> </VisualBrush.Visual> </VisualBrush> </Border.Background> </Border> </Grid> </Grid> </Border> <ContentControl Grid.Row="1" Content="{Binding PageContent}"/> <Border Grid.Row="2" BorderBrush="#5518AABD" BorderThickness="0,1,0,0"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition/> <ColumnDefinition Width="auto"/> </Grid.ColumnDefinitions> <Border Margin="0,5"> <Border.Background> <VisualBrush TileMode="Tile" Viewport="0,0,7,7" ViewportUnits="Absolute"> <VisualBrush.Visual> <Grid Width="20" Height="20"> <Line X1="0" Y1="10" X2="10" Y2="0" Stroke="Gray" StrokeThickness="1"/> </Grid> </VisualBrush.Visual> </VisualBrush> </Border.Background> </Border> <TextBlock Grid.Column="1" Text="2022 ©、®、™ Co. Ltd." Foreground="DarkRed" VerticalAlignment="Center" FontSize="11" FontWeight="Bold" Margin="10,1"/> </Grid> </Border> </Grid> </Window>
TestWindow.xaml.cs
<Style TargetType="Button" x:Key="ButtonStyle"> <Setter Property="Foreground" Value="#AAA"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="Button"> <Border BorderThickness="1" Background="Transparent" Name="border"> <Border.BorderBrush> <LinearGradientBrush StartPoint="0,0" EndPoint="1,0"> <GradientStop Color="#22FFFFFF" Offset="0"/> <GradientStop Color="#77FFFFFF" Offset="0.5"/> <GradientStop Color="#22FFFFFF" Offset="1"/> </LinearGradientBrush> </Border.BorderBrush> <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center"> <TextBlock Text="{TemplateBinding Tag}" FontFamily="Asserts/Fonts/#iconfont" FontSize="14" VerticalAlignment="Center" Margin="0,2,5,0"/> <TextBlock Text="{TemplateBinding Content}" VerticalAlignment="Center"/> </StackPanel> </Border> <ControlTemplate.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter TargetName="border" Property="Background" Value="#11FFFFFF"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> <Style TargetType="Button" x:Key="ControlButtonStyle"> <Setter Property="Width" Value="40"/> <Setter Property="Background" Value="#11FFFFFF"/> <Setter Property="Foreground" Value="White"/> <Setter Property="FontFamily" Value="Asserts/Fonts/#iconfont"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="Button"> <Grid Background="{TemplateBinding Background}"> <Border Name="root"> <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center"/> </Border> </Grid> <ControlTemplate.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter TargetName="root" Property="Background" Value="#33FFFFFF"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> <!--最大化、还原状态|图标切换--> <Style.Triggers> <DataTrigger Binding="{Binding WindowState, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}" Value="Maximized"> <Setter Property="Content" Value=""/> <!----> </DataTrigger> <DataTrigger Binding="{Binding WindowState, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}" Value="Normal"> <Setter Property="Content" Value=""/> <!----> </DataTrigger> </Style.Triggers> </Style> <Style TargetType="Button" x:Key="MinButtonStyle"> <Setter Property="Width" Value="40"/> <Setter Property="Background" Value="#11FFFFFF"/> <Setter Property="Foreground" Value="White"/> <Setter Property="FontFamily" Value="Asserts/Fonts/#iconfont"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="Button"> <Grid Background="{TemplateBinding Background}"> <Border Name="min"> <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center"/> </Border> </Grid> <ControlTemplate.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter TargetName="min" Property="Background" Value="#33FFFFFF"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> <Style TargetType="Button" x:Key="CloseButtonStyle"> <Setter Property="Width" Value="40"/> <Setter Property="Background" Value="#11FFFFFF"/> <Setter Property="Foreground" Value="White"/> <Setter Property="FontFamily" Value="Asserts/Fonts/#iconfont"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="Button"> <Grid Background="{TemplateBinding Background}"> <Border CornerRadius="5,5,5,5" Name="close" > <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center"/> </Border> </Grid> <ControlTemplate.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter TargetName="close" Property="Background" Value="#33FFFFFF"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style>
internal class MainViewModel:NotifyBase { private object _pageContent; public object PageContent { get { return _pageContent; } set { //_pageContent = value; SetProperty(ref _pageContent,value); } } } internal class NotifyBase : INotifyPropertyChanged { public event PropertyChangedEventHandler? PropertyChanged; public void SetProperty<T>(ref T field, T value, [CallerMemberName] string propName = "") { field = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName)); } } public class CommandBase : ICommand { public Action<object>? _execute { get; set; } public Func<object, bool>? _canExecute { get; set; } public event EventHandler? CanExecuteChanged; / public bool CanExecute(object? parameter) { if (_canExecute == null) { return true; } return _canExecute(parameter!); } public void Execute(object? parameter) { if (_execute != null && CanExecute(parameter)) { _execute(parameter!); } } public CommandBase(Action<object> execute, Func<object, bool> canExecute) { _execute = execute; _canExecute = canExecute; } public CommandBase(Action<object> execute) : this(execute, null) { } }