C# WPF 簡單自定義菜單切換動畫

微信公眾號:Dotnet9,網(wǎng)站:Dotnet9,問題或建議,請網(wǎng)站留言粹庞; 如果您覺得Dotnet9對您有幫助,歡迎贊賞

內(nèi)容目錄

實現(xiàn)效果

業(yè)務(wù)場景

編碼實現(xiàn)

本文參考

源碼下載

1.實現(xiàn)效果

自定義菜單切換動畫

2.業(yè)務(wù)場景

菜單切換動畫

3.編碼實現(xiàn)

3.1 添加Nuget庫

使用 .Net Core 3.1 創(chuàng)建名為“CustomMenu”的WPF解決方案洽损,添加兩個Nuget庫:MaterialDesignThemes和MaterialDesignColors。

MaterialDesign控件庫?

3.2 工程結(jié)構(gòu)

只修改了App.xaml(添加MD控件樣式)和MainWindow.xaml(主窗口實現(xiàn)效果)革半。

3.3 App.xaml引入MD控件樣式

<Application x:Class="CustomMenu.App"

? ? ? ? ? ? xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

? ? ? ? ? ? xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

? ? ? ? ? ? xmlns:local="clr-namespace:CustomMenu"

? ? ? ? ? ? StartupUri="MainWindow.xaml">

? ? <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.MergedDictionaries>

? ? ? ? ? ? <!--PRIMARY-->

? ? ? ? ? ? <SolidColorBrush x:Key="PrimaryHueLightBrush" Color="#349FDA"/>

? ? ? ? ? ? <SolidColorBrush x:Key="PrimaryHueLightForegroundBrush" Color="#FF333333"/>

? ? ? ? ? ? <SolidColorBrush x:Key="PrimaryHueMidBrush" Color="#0288d1"/>

? ? ? ? ? ? <SolidColorBrush x:Key="PrimaryHueMidForegroundBrush" Color="#FFDDDDDD"/>

? ? ? ? ? ? <SolidColorBrush x:Key="PrimaryHueDarkBrush" Color="#015f92"/>

? ? ? ? ? ? <SolidColorBrush x:Key="PrimaryHueDarkForegroundBrush" Color="#FFFFFF"/>

? ? ? ? ? ? <!--ACCENT-->

? ? ? ? ? ? <SolidColorBrush x:Key="SecondaryAccentBrush" Color="#FF50F350"/>

? ? ? ? ? ? <SolidColorBrush x:Key="SecondaryAccentForegroundBrush" Color="#FFFFFF"/>

? ? ? ? </ResourceDictionary>

? ? </Application.Resources>

</Application>

3.4 主窗體 MainWindow.xaml

添加菜單碑定、設(shè)置菜單項切換動畫等:

<Window x:Class="CustomMenu.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:CustomMenu"

? ? ? ? xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"

? ? ? ? mc:Ignorable="d"

? ? ? ? Title="MainWindow" Height="600" Width="1080" Background="#FF292929" ResizeMode="NoResize" WindowStyle="None"

? ? ? ? WindowStartupLocation="CenterScreen">

? ? <Window.Resources>

? ? ? ? <Storyboard x:Key="Move0">

? ? ? ? ? ? <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)"

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Storyboard.TargetName="ellipse">

? ? ? ? ? ? ? ? <EasingDoubleKeyFrame KeyTime="0:0:0.4" Value="0">

? ? ? ? ? ? ? ? ? ? <EasingDoubleKeyFrame.EasingFunction>

? ? ? ? ? ? ? ? ? ? ? ? <BackEase EasingMode="EaseInOut"/>

? ? ? ? ? ? ? ? ? ? </EasingDoubleKeyFrame.EasingFunction>

? ? ? ? ? ? ? ? </EasingDoubleKeyFrame>

? ? ? ? ? ? </DoubleAnimationUsingKeyFrames>

? ? ? ? </Storyboard>

? ? ? ? <Storyboard x:Key="Move1">

? ? ? ? ? ? <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)"

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Storyboard.TargetName="ellipse">

? ? ? ? ? ? ? ? <EasingDoubleKeyFrame KeyTime="0:0:0.4" Value="40">

? ? ? ? ? ? ? ? ? ? <EasingDoubleKeyFrame.EasingFunction>

? ? ? ? ? ? ? ? ? ? ? ? <BackEase EasingMode="EaseInOut"/>

? ? ? ? ? ? ? ? ? ? </EasingDoubleKeyFrame.EasingFunction>

? ? ? ? ? ? ? ? </EasingDoubleKeyFrame>

? ? ? ? ? ? </DoubleAnimationUsingKeyFrames>

? ? ? ? </Storyboard>

? ? ? ? <Storyboard x:Key="Move2">

? ? ? ? ? ? <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)"

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Storyboard.TargetName="ellipse">

? ? ? ? ? ? ? ? <EasingDoubleKeyFrame KeyTime="0:0:0.4" Value="80">

? ? ? ? ? ? ? ? ? ? <EasingDoubleKeyFrame.EasingFunction>

? ? ? ? ? ? ? ? ? ? ? ? <BackEase EasingMode="EaseInOut"/>

? ? ? ? ? ? ? ? ? ? </EasingDoubleKeyFrame.EasingFunction>

? ? ? ? ? ? ? ? </EasingDoubleKeyFrame>

? ? ? ? ? ? </DoubleAnimationUsingKeyFrames>

? ? ? ? </Storyboard>

? ? </Window.Resources>

? ? <Window.Triggers>

? ? ? ? <EventTrigger RoutedEvent="ListBoxItem.Selected" SourceName="item0">

? ? ? ? ? ? <BeginStoryboard x:Name="Move0_BeginStoryboard" Storyboard="{StaticResource Move0}"/>

? ? ? ? </EventTrigger>

? ? ? ? <EventTrigger RoutedEvent="ListBoxItem.Selected" SourceName="item1">

? ? ? ? ? ? <BeginStoryboard x:Name="Move1_BeginStoryboard" Storyboard="{StaticResource Move1}"/>

? ? ? ? </EventTrigger>

? ? ? ? <EventTrigger RoutedEvent="ListBoxItem.Selected" SourceName="item2">

? ? ? ? ? ? <BeginStoryboard x:Name="Move2_BeginStoryboard" Storyboard="{StaticResource Move2}"/>

? ? ? ? </EventTrigger>

? ? </Window.Triggers>


? ? <Grid>

? ? ? ? <Grid.RowDefinitions>

? ? ? ? ? ? <RowDefinition Height="40"/>

? ? ? ? ? ? <RowDefinition Height="*"/>

? ? ? ? </Grid.RowDefinitions>

? ? ? ? <Border Grid.Row="0" BorderBrush="{StaticResource SecondaryAccentBrush}" BorderThickness="0 0 0 1">

? ? ? ? ? ? <StackPanel HorizontalAlignment="Right" VerticalAlignment="Center" Orientation="Horizontal">

? ? ? ? ? ? ? ? <Button Style="{StaticResource MaterialDesignFlatButton}">

? ? ? ? ? ? ? ? ? ? <materialDesign:PackIcon Kind="Minus"/>

? ? ? ? ? ? ? ? </Button>

? ? ? ? ? ? ? ? <Button Style="{StaticResource MaterialDesignFlatButton}">

? ? ? ? ? ? ? ? ? ? <materialDesign:PackIcon Kind="Close"/>

? ? ? ? ? ? ? ? </Button>

? ? ? ? ? ? </StackPanel>

? ? ? ? </Border>

? ? ? ? <Grid Grid.Row="1">

? ? ? ? ? ? <Grid.ColumnDefinitions>

? ? ? ? ? ? ? ? <ColumnDefinition Width="200"/>

? ? ? ? ? ? ? ? <ColumnDefinition Width="*"/>

? ? ? ? ? ? </Grid.ColumnDefinitions>

? ? ? ? ? ? <ListView Width="200" HorizontalAlignment="Left">

? ? ? ? ? ? ? ? <ListViewItem x:Name="item0" Content="首頁" Height="40"/>

? ? ? ? ? ? ? ? <ListViewItem x:Name="item1" Content="瀏覽" Height="40"/>

? ? ? ? ? ? ? ? <ListViewItem x:Name="item2" Content="視頻" Height="40"/>

? ? ? ? ? ? </ListView>

? ? ? ? ? ? <Grid Grid.ColumnSpan="2" Width="5" Margin="195 0" HorizontalAlignment="Left">

? ? ? ? ? ? ? ? <Rectangle Fill="{StaticResource SecondaryAccentBrush}" Width="2" HorizontalAlignment="Right"/>

? ? ? ? ? ? ? ? <Ellipse x:Name="ellipse" Width="10" Height="10" VerticalAlignment="Top" Fill="{StaticResource SecondaryAccentBrush}" Margin="0 15"

? ? ? ? ? ? ? ? ? ? ? ? RenderTransformOrigin="0.5 0.5">

? ? ? ? ? ? ? ? ? ? <Ellipse.RenderTransform>

? ? ? ? ? ? ? ? ? ? ? ? <TransformGroup>

? ? ? ? ? ? ? ? ? ? ? ? ? ? <ScaleTransform/>

? ? ? ? ? ? ? ? ? ? ? ? ? ? <SkewTransform/>

? ? ? ? ? ? ? ? ? ? ? ? ? ? <RotateTransform/>

? ? ? ? ? ? ? ? ? ? ? ? ? ? <TranslateTransform/>

? ? ? ? ? ? ? ? ? ? ? ? </TransformGroup>

? ? ? ? ? ? ? ? ? ? </Ellipse.RenderTransform>

? ? ? ? ? ? ? ? </Ellipse>

? ? ? ? ? ? </Grid>

? ? ? ? </Grid>

? ? </Grid>

</Window>

4.本文參考

Design com WPF大神的學(xué)習(xí)視頻:CustomMenu

開源控件庫:MaterialDesignInXamlToolkit

本站對MD開源控件庫的介紹:控件介紹

5.代碼下載

Github源碼下載:下載

除非注明,文章均由?Dotnet9?整理發(fā)布又官,歡迎轉(zhuǎn)載延刘。

轉(zhuǎn)載請注明本文地址:https://dotnet9.com/6820.html

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市六敬,隨后出現(xiàn)的幾起案子碘赖,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,546評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件普泡,死亡現(xiàn)場離奇詭異播掷,居然都是意外死亡,警方通過查閱死者的電腦和手機撼班,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,224評論 3 395
  • 文/潘曉璐 我一進(jìn)店門歧匈,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人砰嘁,你說我怎么就攤上這事件炉。” “怎么了矮湘?”我有些...
    開封第一講書人閱讀 164,911評論 0 354
  • 文/不壞的土叔 我叫張陵斟冕,是天一觀的道長。 經(jīng)常有香客問我缅阳,道長磕蛇,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,737評論 1 294
  • 正文 為了忘掉前任券时,我火速辦了婚禮孤里,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘橘洞。我一直安慰自己捌袜,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,753評論 6 392
  • 文/花漫 我一把揭開白布炸枣。 她就那樣靜靜地躺著虏等,像睡著了一般。 火紅的嫁衣襯著肌膚如雪适肠。 梳的紋絲不亂的頭發(fā)上霍衫,一...
    開封第一講書人閱讀 51,598評論 1 305
  • 那天,我揣著相機與錄音侯养,去河邊找鬼敦跌。 笑死,一個胖子當(dāng)著我的面吹牛逛揩,可吹牛的內(nèi)容都是我干的柠傍。 我是一名探鬼主播,決...
    沈念sama閱讀 40,338評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼辩稽,長吁一口氣:“原來是場噩夢啊……” “哼惧笛!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起逞泄,我...
    開封第一講書人閱讀 39,249評論 0 276
  • 序言:老撾萬榮一對情侶失蹤患整,失蹤者是張志新(化名)和其女友劉穎拜效,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體各谚,經(jīng)...
    沈念sama閱讀 45,696評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡紧憾,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,888評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了嘲碧。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片稻励。...
    茶點故事閱讀 40,013評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖愈涩,靈堂內(nèi)的尸體忽然破棺而出望抽,到底是詐尸還是另有隱情,我是刑警寧澤履婉,帶...
    沈念sama閱讀 35,731評論 5 346
  • 正文 年R本政府宣布煤篙,位于F島的核電站,受9級特大地震影響毁腿,放射性物質(zhì)發(fā)生泄漏辑奈。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,348評論 3 330
  • 文/蒙蒙 一已烤、第九天 我趴在偏房一處隱蔽的房頂上張望鸠窗。 院中可真熱鬧,春花似錦胯究、人聲如沸稍计。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,929評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽臣嚣。三九已至,卻和暖如春剥哑,著一層夾襖步出監(jiān)牢的瞬間硅则,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,048評論 1 270
  • 我被黑心中介騙來泰國打工株婴, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留怎虫,地道東北人。 一個月前我還...
    沈念sama閱讀 48,203評論 3 370
  • 正文 我出身青樓困介,卻偏偏與公主長得像揪垄,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子逻翁,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,960評論 2 355