要
WPF中的三個帶標題的內容控件,分別是GroupBox,TabControl和Expander
正文
GroupBox 控件將允許您直觀地將一組控件組合在一起。這顯然也可以使用許多面板中的一個來完成,但是 GroupBox 添加了一種特殊類型的標題和邊框,這在歷史上在 Windows 操作系統中被大量使用。
<Window x:Class="_16.MainWindow"
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:_16"
mc:Ignorable="d"
Title="MainWindow" Height="220" Width="300" ResizeMode="NoResize" WindowStyle="SingleBorderWindow">
<Grid>
<GroupBox Margin="10" Padding="10">
<GroupBox.Header>
<StackPanel Orientation="Horizontal">
<Image Source="/user.png" Width="16"></Image>
<TextBlock FontWeight="Bold">用戶登錄</TextBlock>
</StackPanel>
</GroupBox.Header>
<StackPanel>
<TextBlock>用戶名</TextBlock>
<TextBox></TextBox>
<TextBlock>密碼</TextBlock>
<PasswordBox></PasswordBox>
<StackPanel Orientation="Horizontal">
<Button Margin="0 10" Padding="5" Content="登錄"></Button>
<Button Margin="10" Padding="5" Content="關閉"></Button>
</StackPanel>
</StackPanel>
</GroupBox>
</Grid>
</Window>
TabControl控件 允許您將界面分成不同的區域,每個區域都可以通過單擊通常位于控件頂部的選項卡標題來訪問。選項卡控件通常用于 Windows 應用程序,甚至在 Windows 自己的界面中,如文件/文件夾的屬性對話框等。
<Grid>
<TabControl Margin="10" TabStripPlacement="Top"
Name="tabMain" SelectionChanged="tabMain_SelectionChanged">
<TabItem Header="A">
<StackPanel Margin="5">
<CheckBox>A1</CheckBox>
<CheckBox>A2</CheckBox>
<CheckBox>A3</CheckBox>
</StackPanel>
</TabItem>
<TabItem>
<TabItem.Header>
<StackPanel Orientation="Horizontal">
<Image Source="/user.png" Width="16" Stretch="Fill"></Image>
<TextBlock>用戶信息</TextBlock>
</StackPanel>
</TabItem.Header>
<TabItem.Content>
<StackPanel>
<Button x:Name="btnGo" Margin="10" Click="btnGo_Click" Content="GoC"></Button>
</StackPanel>
</TabItem.Content>
</TabItem>
<TabItem Header="C">
CC
</TabItem>
</TabControl>
</Grid>
private void tabMain_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
this.Dispatcher.BeginInvoke(new Action(() => {
MessageBox.Show(tabMain.SelectedIndex.ToString());
}));
}
private void btnGo_Click(object sender, RoutedEventArgs e)
{
tabMain.SelectedIndex = 2;
}
Expander控件,它封裝了一塊內容,通過單擊小箭頭按鈕可現實或隱藏所包含的內容。Expander控件默認折疊,可通過設置IsExpandered屬性改變這種行為。
可設置擴展器的擴展方向,默認為Down,通過設置ExpandDirection屬性的Up、Left或Right值。
當折疊Expander時,箭頭始終指向將要展開的方向。
使用ScrollViewer控件創建可滾動的擴展區域。
信公眾號:Dotnet9,網站:Dotnet9,問題或建議:請網站留言, 如果對您有所幫助:歡迎贊賞。
閱讀導航
YouTube上老外的一個設計,站長覺得不錯,分享給大家作為參考,抽屜菜單的動畫做的非常不錯。
運行起始界面:
站長運行操作一遍,錄制了動畫大家看看:
使用 .NET CORE 3.1 創建名為 “AnimatedMenu” 的WPF模板項目,添加1個Nuget庫:MaterialDesignThemes,版本為最新預覽版3.1.0-ci948。
解決方案主要文件目錄組織結構:
文件【App.xaml】,在 StartupUri 中設置啟動的視圖【MainWindow.xaml】,并在【Application.Resources】節點增加 MaterialDesignThemes庫的樣式文件:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Dark.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.Blue.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Blue.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
文件【MainWindow.xaml】,布局代碼、動畫代碼都在此文件中,源碼如下:
<Window x:Class="AnimatedMenu.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" MouseLeftButtonDown="MoveWindow_MouseLeftButtonDown"
Height="600" Width="1024" WindowStyle="None" WindowStartupLocation="CenterScreen">
<WindowChrome.WindowChrome>
<WindowChrome CaptionHeight="0"/>
</WindowChrome.WindowChrome>
<Window.Resources>
<Storyboard x:Key="OpenMenu">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)" Storyboard.TargetName="GridMain">
<EasingDoubleKeyFrame KeyTime="0" Value="0"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="250"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)" Storyboard.TargetName="GridMain">
<EasingDoubleKeyFrame KeyTime="0" Value="0"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="50"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)" Storyboard.TargetName="StackPanelMenu">
<EasingDoubleKeyFrame KeyTime="0" Value="0"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="250"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)" Storyboard.TargetName="listViewItem">
<EasingDoubleKeyFrame KeyTime="0" Value="-250"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="-250"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.7" Value="0"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)" Storyboard.TargetName="listViewItem1">
<EasingDoubleKeyFrame KeyTime="0" Value="-250"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="-250"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.9" Value="0"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)" Storyboard.TargetName="listViewItem2">
<EasingDoubleKeyFrame KeyTime="0" Value="-250"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="-250"/>
<EasingDoubleKeyFrame KeyTime="0:0:1.1" Value="0"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)" Storyboard.TargetName="listViewItem3">
<EasingDoubleKeyFrame KeyTime="0" Value="-250"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="-250"/>
<EasingDoubleKeyFrame KeyTime="0:0:1.3" Value="0"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)" Storyboard.TargetName="listViewItem4">
<EasingDoubleKeyFrame KeyTime="0" Value="-250"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="-250"/>
<EasingDoubleKeyFrame KeyTime="0:0:1.5" Value="0"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)" Storyboard.TargetName="button">
<EasingDoubleKeyFrame KeyTime="0" Value="0"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="0"/>
<EasingDoubleKeyFrame KeyTime="0:0:1.5" Value="1"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)" Storyboard.TargetName="button">
<EasingDoubleKeyFrame KeyTime="0" Value="0"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="0"/>
<EasingDoubleKeyFrame KeyTime="0:0:1.5" Value="1"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
<Storyboard x:Key="CloseMenu">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)" Storyboard.TargetName="GridMain">
<EasingDoubleKeyFrame KeyTime="0" Value="250"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="0"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)" Storyboard.TargetName="GridMain">
<EasingDoubleKeyFrame KeyTime="0" Value="50"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="0"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)" Storyboard.TargetName="StackPanelMenu">
<EasingDoubleKeyFrame KeyTime="0" Value="250"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="0"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</Window.Resources>
<Window.Triggers>
<EventTrigger RoutedEvent="ButtonBase.Click" SourceName="ButtonOpenMenu">
<BeginStoryboard Storyboard="{StaticResource OpenMenu}"/>
</EventTrigger>
<EventTrigger RoutedEvent="ButtonBase.Click" SourceName="ButtonCloseMenu">
<BeginStoryboard x:Name="CloseMenu_BeginStoryboard" Storyboard="{StaticResource CloseMenu}"/>
</EventTrigger>
</Window.Triggers>
<Grid Background="#FF3580BF">
<StackPanel x:Name="StackPanelMenu" Width="250" HorizontalAlignment="Left" Margin="-250 0 0 0" RenderTransformOrigin="0.5,0.5">
<StackPanel.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</StackPanel.RenderTransform>
<StackPanel Orientation="Horizontal" VerticalAlignment="Top" Height="100" HorizontalAlignment="Center">
<Button Style="{StaticResource MaterialDesignFloatingActionMiniAccentButton}" Background="{x:Null}" BorderBrush="{x:Null}" Padding="0" Width="50" Height="50" Margin="10">
<materialDesign:PackIcon Kind="Settings" Width="40" Height="40"/>
</Button>
<Button x:Name="button" Style="{StaticResource MaterialDesignFloatingActionMiniAccentButton}" BorderBrush="{x:Null}" Padding="0" Width="80" Height="80" Margin="10" RenderTransformOrigin="0.5,0.5">
<Button.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</Button.RenderTransform>
<Button.Background>
<ImageBrush ImageSource="https://img.dotnet9.com/logo.png" Stretch="UniformToFill"/>
</Button.Background>
</Button>
<Button Style="{StaticResource MaterialDesignFloatingActionMiniAccentButton}" Background="{x:Null}" BorderBrush="{x:Null}" Padding="0" Width="50" Height="50" Margin="10">
<materialDesign:PackIcon Kind="InformationOutline" Width="40" Height="40"/>
</Button>
</StackPanel>
<ListView>
<ListViewItem x:Name="listViewItem" Height="60" RenderTransformOrigin="0.5,0.5">
<ListViewItem.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</ListViewItem.RenderTransform>
<StackPanel Orientation="Horizontal" Margin="10 0">
<materialDesign:PackIcon Kind="Home" Width="30" Height="30" VerticalAlignment="Center" Margin="5"/>
<TextBlock Text="主頁" Margin="10" VerticalAlignment="Center"/>
</StackPanel>
</ListViewItem>
<ListViewItem x:Name="listViewItem1" Height="60" RenderTransformOrigin="0.5,0.5">
<ListViewItem.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</ListViewItem.RenderTransform>
<StackPanel Orientation="Horizontal" Margin="10 0">
<materialDesign:PackIcon Kind="AccountSearch" Width="30" Height="30" VerticalAlignment="Center" Margin="5"/>
<TextBlock Text="搜索" Margin="10" VerticalAlignment="Center"/>
</StackPanel>
</ListViewItem>
<ListViewItem x:Name="listViewItem2" Height="60" RenderTransformOrigin="0.5,0.5">
<ListViewItem.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</ListViewItem.RenderTransform>
<StackPanel Orientation="Horizontal" Margin="10 0">
<materialDesign:PackIcon Kind="Wechat" Width="30" Height="30" VerticalAlignment="Center" Margin="5"/>
<TextBlock Text="微信" Margin="10" VerticalAlignment="Center"/>
</StackPanel>
</ListViewItem>
<ListViewItem x:Name="listViewItem3" Height="60" RenderTransformOrigin="0.5,0.5">
<ListViewItem.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</ListViewItem.RenderTransform>
<StackPanel Orientation="Horizontal" Margin="10 0">
<materialDesign:PackIcon Kind="Qqchat" Width="30" Height="30" VerticalAlignment="Center" Margin="5"/>
<TextBlock Text="QQ" Margin="10" VerticalAlignment="Center"/>
</StackPanel>
</ListViewItem>
<ListViewItem x:Name="listViewItem4" Height="60" RenderTransformOrigin="0.5,0.5">
<ListViewItem.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</ListViewItem.RenderTransform>
<StackPanel Orientation="Horizontal" Margin="10 0">
<materialDesign:PackIcon Kind="Facebook" Width="30" Height="30" VerticalAlignment="Center" Margin="5"/>
<TextBlock Text="臉書" Margin="10" VerticalAlignment="Center"/>
</StackPanel>
</ListViewItem>
</ListView>
</StackPanel>
<Grid x:Name="GridMain" Background="#FFFBFBFB" Width="1024" RenderTransformOrigin="0.5,0.5">
<Grid.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</Grid.RenderTransform>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="250"/>
</Grid.ColumnDefinitions>
<Grid Grid.Column="1" Background="#FF3580BF">
<Image Height="150" VerticalAlignment="Top" Source="https://dotnet9.com/wp-content/uploads/2017/04/About-Header.jpg" Stretch="UniformToFill"/>
<Ellipse Height="100" Width="100" VerticalAlignment="Top" HorizontalAlignment="Right" Margin="20 100" Stroke="White">
<Ellipse.Fill>
<ImageBrush ImageSource="https://img.dotnet9.com/logo.png" Stretch="UniformToFill"/>
</Ellipse.Fill>
</Ellipse>
<TextBlock Text="Dotnet9" Foreground="White" FontSize="28" FontFamily="Nirmala UI Semilight" Margin="10 100" VerticalAlignment="Top"/>
<StackPanel Margin="0 150">
<Grid Height="60" Margin="20 50 20 0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<TextBlock Text="追隨者" VerticalAlignment="Bottom" Foreground="#FFFBFBFB" Margin="5,0,5,5"/>
<TextBlock Text="1.5K" VerticalAlignment="Top" Foreground="#FFFBFBFB" Grid.Row="1" Margin="10 0"/>
<TextBlock Text="跟隨" VerticalAlignment="Bottom" Foreground="#FFFBFBFB" Margin="5,0,5,5" Grid.Column="1"/>
<TextBlock Text="2.3K" VerticalAlignment="Top" Foreground="#FFFBFBFB" Grid.Row="1" Margin="10 0" Grid.Column="1"/>
</Grid>
<TextBlock TextWrapping="Wrap" Margin="10" Foreground="#FFFBFBFB" FontSize="14">
<Run Text="網名:沙漠盡頭的狼"/>
<LineBreak/>
<LineBreak/>
<Run Text="網站:https://dotnet9.com"/>
<LineBreak/>
<Run Text=" Dotnet9的博客 - 一個熱衷于互聯網分享精神的個人博客站點"/>
<LineBreak/>
<LineBreak/>
<Run Text="座右銘:時間如流水,只能流去不流回。"/>
</TextBlock>
</StackPanel>
</Grid>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="40"/>
<RowDefinition Height="50"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Button x:Name="ButtonCloseMenu" Style="{StaticResource MaterialDesignFloatingActionMiniAccentButton}" Width="30" Height="30" Padding="0" Background="{x:Null}" BorderBrush="{x:Null}" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="5" Click="ButtonCloseMenu_Click" Visibility="Collapsed">
<materialDesign:PackIcon Kind="Menu" Foreground="#FF3580BF"/>
</Button>
<Button x:Name="ButtonOpenMenu" Style="{StaticResource MaterialDesignFloatingActionMiniAccentButton}" Width="30" Height="30" Padding="0" Background="{x:Null}" BorderBrush="{x:Null}" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="5" Click="ButtonOpenMenu_Click">
<materialDesign:PackIcon Kind="Menu" Foreground="#FF3580BF"/>
</Button>
<TextBlock Text="照片" Foreground="#FF3580BF" FontSize="30" FontWeight="Bold" Margin="5" Grid.Row="1"/>
<Grid Margin="5" Grid.Row="2" Grid.Column="0">
<Grid.Effect>
<DropShadowEffect BlurRadius="20" Color="#FFEEEEEE" ShadowDepth="1"/>
</Grid.Effect>
<Image Source="https://dotnet9.com/wp-content/uploads/2019/12/wpf.png" Stretch="UniformToFill"/>
<StackPanel Orientation="Horizontal" Margin="5" HorizontalAlignment="Right" VerticalAlignment="Bottom">
<materialDesign:PackIcon Kind="Heart" Foreground="#FFFBFBFB"/>
<TextBlock Text="25" Foreground="#FFFBFBFB"/>
</StackPanel>
</Grid>
<Grid Margin="5" Grid.Row="2" Grid.Column="1">
<Grid.Effect>
<DropShadowEffect BlurRadius="20" Color="#FFEEEEEE" ShadowDepth="1"/>
</Grid.Effect>
<Image Source="https://dotnet9.com/wp-content/uploads/2019/12/winform.png" Stretch="UniformToFill"/>
<StackPanel Orientation="Horizontal" Margin="5" HorizontalAlignment="Right" VerticalAlignment="Bottom">
<materialDesign:PackIcon Kind="Heart" Foreground="#FFFBFBFB"/>
<TextBlock Text="50" Foreground="#FFFBFBFB"/>
</StackPanel>
</Grid>
<Grid Margin="5" Grid.Row="2" Grid.Column="2">
<Grid.Effect>
<DropShadowEffect BlurRadius="20" Color="#FFEEEEEE" ShadowDepth="1"/>
</Grid.Effect>
<Image Source="https://dotnet9.com/wp-content/uploads/2019/12/asp-net-core.png" Stretch="UniformToFill"/>
<StackPanel Orientation="Horizontal" Margin="5" HorizontalAlignment="Right" VerticalAlignment="Bottom">
<materialDesign:PackIcon Kind="Heart" Foreground="#FFFBFBFB"/>
<TextBlock Text="18" Foreground="#FFFBFBFB"/>
</StackPanel>
</Grid>
<Grid Margin="5" Grid.Row="3" Grid.Column="0">
<Grid.Effect>
<DropShadowEffect BlurRadius="20" Color="#FFEEEEEE" ShadowDepth="1"/>
</Grid.Effect>
<Image Source="https://img.dotnet9.com/Xamarin.Forms.png" Stretch="UniformToFill"/>
<StackPanel Orientation="Horizontal" Margin="5" HorizontalAlignment="Right" VerticalAlignment="Bottom">
<materialDesign:PackIcon Kind="Heart" Foreground="#FFFBFBFB"/>
<TextBlock Text="32" Foreground="#FFFBFBFB"/>
</StackPanel>
</Grid>
<Grid Margin="5" Grid.Row="3" Grid.Column="1">
<Grid.Effect>
<DropShadowEffect BlurRadius="20" Color="#FFEEEEEE" ShadowDepth="1"/>
</Grid.Effect>
<Image Source="https://dotnet9.com/wp-content/uploads/2019/12/front-end.png" Stretch="UniformToFill"/>
<StackPanel Orientation="Horizontal" Margin="5" HorizontalAlignment="Right" VerticalAlignment="Bottom">
<materialDesign:PackIcon Kind="Heart" Foreground="#FFFBFBFB"/>
<TextBlock Text="32" Foreground="#FFFBFBFB"/>
</StackPanel>
</Grid>
</Grid>
</Grid>
<StackPanel Orientation="Horizontal" VerticalAlignment="Top" Height="40" HorizontalAlignment="Right" Margin="10">
<Button Style="{StaticResource MaterialDesignFloatingActionMiniAccentButton}" Width="30" Height="30" Padding="0" Background="{x:Null}" BorderBrush="{x:Null}">
<materialDesign:PackIcon Kind="Bell"/>
</Button>
<Button x:Name="ButtonClose" Style="{StaticResource MaterialDesignFloatingActionMiniAccentButton}" Width="30" Height="30" Padding="0" Background="{x:Null}" BorderBrush="{x:Null}" Click="ButtonClose_Click">
<materialDesign:PackIcon Kind="Close"/>
</Button>
</StackPanel>
</Grid>
</Window>
簡單說明下:
文件【MainWindow.xaml.cs】,后臺關閉窗體、抽屜菜單按鈕切換、窗體移動等事件處理:
private void ButtonClose_Click(object sender, RoutedEventArgs e)
{
Application.Current.Shutdown();
}
private void ButtonOpenMenu_Click(object sender, RoutedEventArgs e)
{
ButtonOpenMenu.Visibility = Visibility.Collapsed;
ButtonCloseMenu.Visibility = Visibility.Visible;
}
private void ButtonCloseMenu_Click(object sender, RoutedEventArgs e)
{
ButtonOpenMenu.Visibility = Visibility.Visible;
ButtonCloseMenu.Visibility = Visibility.Collapsed;
}
private void MoveWindow_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
DragMove();
}
效果圖實現代碼在文中已經全部給出,站長方便演示,文中的圖片使用的本站外鏈圖片,代碼可直接Copy,按解決方案目錄組織代碼文件即可運行。
演示Demo下載
除非注明,文章均由 Dotnet9 整理發布,歡迎轉載。轉載請注明本文地址:https://dotnet9.com/7669.html歡迎掃描下方二維碼關注 Dotnet9 的微信公眾號,本站會及時推送最新技術文章
時間如流水,只能流去不流回!
點擊《【閱讀原文】》,本站還有更多技術類文章等著您哦!!!
述:在WPF中,通過從默認樣式繼承,實現了靈活的控件樣式定制。基礎用法展示了按鈕樣式的簡單繼承,而高級用法通過自定義樣式同時定制了默認和特定控件樣式,為WPF應用提供了更靈活的外觀定制方式
在WPF(Windows Presentation Foundation)中,可以通過從默認樣式繼承來定制控件樣式。以下是詳細講解和不同實例的說明,包括基礎和高級用法。
<!-- 自定義按鈕樣式,繼承自默認按鈕樣式 -->
<Style x:Key="CustomButtonStyle" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
<Setter Property="Background" Value="LightBlue"/>
<Setter Property="Foreground" Value="DarkBlue"/>
</Style>
<!-- 應用自定義按鈕樣式 -->
<Button Content="Click me" Style="{StaticResource CustomButtonStyle}"/>
<!-- 自定義文本框樣式,繼承自默認文本框樣式 -->
<Style x:Key="CustomTextBoxStyle" TargetType="TextBox" BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="Background" Value="LightYellow"/>
<Setter Property="BorderBrush" Value="DarkGoldenrod"/>
<Setter Property="BorderThickness" Value="2"/>
</Style>
<!-- 在資源字典中定義默認文本框樣式 -->
<ResourceDictionary>
<Style TargetType="TextBox">
<Setter Property="Background" Value="White"/>
<Setter Property="BorderBrush" Value="Black"/>
<Setter Property="BorderThickness" Value="1"/>
</Style>
</ResourceDictionary>
<!-- 在應用中使用自定義文本框樣式 -->
<TextBox Text="Type here" Style="{StaticResource CustomTextBoxStyle}"/>
通過這些例子,你可以清晰地了解如何在WPF中通過繼承默認樣式來定制控件外觀。這樣的設計方式使得樣式的維護和修改更加方便。
源代碼獲取:私我
*請認真填寫需求信息,我們會在24小時內與您取得聯系。