添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
强健的皮带  ·  VICUNA - 知乎·  1 年前    · 
鬼畜的登山鞋  ·  java - Failed to ...·  1 年前    · 
欢乐的灭火器  ·  Forbidden (403) CSRF ...·  1 年前    · 
  1. 实现效果
  2. 业务场景
  3. 编码实现
  4. 本文参考
  5. 源码下载

1.实现效果

效果
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-JDObzpGM-1578370393888)(https://img.dotnet9.com/20200107114801.gif ‘效果’)]

2.业务场景

3.编码实现

3.1 添加Nuget库

使用 .Net Core 3.1 创建名为 “ResponsiveLayout” 的WPF解决方案,添加两个Nuget库:MaterialDesignThemes和MaterialDesignColors。

MaterialDesign控件库
MaterialDesign

3.2 工程结构

3个文件变动:

  1. App.xaml:添加MD控件样式
  2. MainWindow.xaml:主窗口实现效果
  3. MainWindow.xaml.cs:主窗口后台实现抽屉菜单开和闭

3.3 App.xaml引入MD控件样式

关键样式引用代码

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.DeepPurple.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Lime.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

3.4 主窗体 MainWindow.xaml

全部代码,菜单及右侧布局

<Window x:Class="ResponsiveLayout.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:ResponsiveLayout"
        xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <Storyboard x:Key="CloseMenu">
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(Grid.Width)" Storyboard.TargetName="grid">
                <EasingDoubleKeyFrame KeyTime="0" Value="200"/>
                <EasingDoubleKeyFrame KeyTime="0:0:0.4" Value="70"/>
            </DoubleAnimationUsingKeyFrames>
        </Storyboard>
        <Storyboard x:Key="OpenMenu">
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(Grid.Width)" Storyboard.TargetName="grid">
                <EasingDoubleKeyFrame KeyTime="0" Value="70"/>
                <EasingDoubleKeyFrame KeyTime="0:0:0.4" Value="200"/>
            </DoubleAnimationUsingKeyFrames>
        </Storyboard>
    </Window.Resources>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="auto"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Grid Background="#FFCBCBCB" Grid.Column="1">
                <Grid Margin="0 20 0 0" Background="#FFEEEEEE">
                    <Grid Height="100" Background="#FFEEEEEE" VerticalAlignment="Top" HorizontalAlignment="Stretch" Margin="10">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>
                        <Border BorderBrush="White" BorderThickness="0 0 1 0" Grid.Column="0">
                            <TextBlock FontSize="30" Text="R$ 860,90" Foreground="Black" Margin="15"/>
                        </Border>
                        <Border BorderBrush="White" BorderThickness="0 0 1 0" Grid.Column="1">
                            <TextBlock FontSize="30" Text="R$ 750,90" Foreground="Black" Margin="15"/>
                        </Border>
                        <Border BorderBrush="White" BorderThickness="0 0 1 0" Grid.Column="2">
                            <TextBlock FontSize="30" Text="R$ 60,90" Foreground="Black" Margin="15"/>
                        </Border>
                        <Border BorderBrush="White" BorderThickness="0 0 1 0" Grid.Column="3">
                            <TextBlock FontSize="30" Text="R$ 865,90" Foreground="Black" Margin="15"/>
                        </Border>
                    </Grid>
                </Grid>
            </Grid>
            <Grid x:Name="grid" Width="200" Background="#FF6C6C8D" RenderTransformOrigin="0.5,0.5" Grid.Column="0">
                <Button x:Name="button" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="10" Style="{StaticResource MaterialDesignFlatButton}" Click="Button_Click">
                    <materialDesign:PackIcon Kind="Menu" Foreground="White"/>
                </Button>
            </Grid>
        </Grid>
    </Grid>
</Window>

3.5 MainWindow.xaml.cs

关键代码,简单的菜单开、闭动画播放

private void Button_Click(object sender, RoutedEventArgs e)
    if (MenuClosed)
        Storyboard openMenu = (Storyboard)button.FindResource("OpenMenu");
        openMenu.Begin();
        Storyboard closeMenu = (Storyboard)button.FindResource("CloseMenu");
        closeMenu.Begin();
    MenuClosed = !MenuClosed;

4.本文参考

Design com WPF 大神的学习视频:Responsive Layout and Menu Navigation Drawer

开源控件库:MaterialDesignInXamlToolkit

本站对MD开源控件库的介绍:控件介绍

5.代码下载

Github源码下载:ResponsiveLayout

除非注明,文章均由 Dotnet9 整理发布,欢迎转载。

转载请注明本文地址:https://dotnet9.com/6833.html

欢迎扫描下方二维码关注 Dotnet9 的微信公众号,本站会及时推送最新技术文章

Dotnet9

微信公众号:Dotnet9,网站:Dotnet9,问题或建议,请网站留言;如果您觉得Dotnet9对您有帮助,欢迎赞赏C# WPF 响应式布局和抽屉式菜单导航内容目录实现效果业务场景编码实现本文参考源码下载1.实现效果效果[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-JDObzpGM-1578370393888)(https://img... <mah:MetroWindow x:Class="Kx.View.MyMainView" 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="http://schemas.microsoft.com/winfx/2006/xaml/presentation"         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"         Title="MainWindow" Height="350" Width="525">
1整体项目基于WPF的MVVM框架,分为上左中三部分,可伸缩; 2仿Extjs的左菜单,可动态绑定数据集,采用fontawesome作为图片; 3左菜单联动的中间部分的Tab控件,Tab页可关闭。 补充:该项目用vs2012运行。 准备修改积分的,原代码找不到了,麻烦管理员将这个资源删除,谢谢!
一、添加两个Nuget库:MaterialDesignThemes和MaterialDesignColors; 二、在App.xaml中添加MaterialDesignInXaml样式; <Application x:Class="WPFTest0615.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http
WPF导航和顶部菜单联动主要是通过控制菜单的选择来实现导航的功能。我们可以在顶部菜单上设置一系列选项,每个选项对应着一个页面或者视图,当用户选择某个选项时,程序会根据选项的值来展示相应的页面或视图。 在WPF中,我们可以利用导航框架来实现页面的导航导航框架类似于浏览器的前进和后退功能,能够方便地切换页面,并且支持导航历史记录。我们可以将导航框架放在主窗口的页面容器中,然后根据用户选择的菜单选项来动态加载相应的页面。 具体实现如下: 1. 在主窗口上添加一个菜单,并为每个菜单项设置一个命令。命令的执行器应该是通过导航框架来实现页面的导航。 2. 为了避免在每个页面中都添加导航框架,我们可以定义一个继承自导航窗口并带有导航框架的容器页面。每次导航时,将要导航的页面加载到容器页面的导航框架中。 3. 利用事件或其他机制,让菜单项可以控制导航框架的导航操作。 4. 如果需要对导航进行更多的定制,可以利用导航事件处理程序、导航状态等WPF提供的导航相关事件。 总的来说,WPF导航和顶部菜单联动实现起来并不是很复杂,只需要合理地运用WPF导航框架和控制机制即可。