最近项目中有这么个需求:

菜单栏滚动到顶部后固定在顶部,专业的名词叫吸顶。。这在移动端还是比较常见的。

看看效果:

下面直接看看代码喽,代码不多:

第一种方法思路:

写一个和菜单一模一样的菜单,放在窗口顶部隐藏,当菜单滚动到上方时,就显示出来,反之隐藏。

MainWindow1.xaml如下:

<Window x:Class="wpfcore.MainWindow1"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:wpfcore"mc:Ignorable="d"Title="MainWindow1" Height="450" Width="800"><Grid><ScrollViewer ScrollChanged="ScrollViewer_ScrollChanged"><Grid><Grid.RowDefinitions><RowDefinition Height="200"/><RowDefinition Height="auto"/><RowDefinition Height="*"/></Grid.RowDefinitions><Grid Grid.Row="0" x:Name="banner"><Image Source="D:\bizhi\国漫\2-9.jpg" Stretch="UniformToFill" HorizontalAlignment="Center" VerticalAlignment="Center"/><TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="30" Foreground="White" Text="这是顶部Banner"/></Grid><StackPanel Grid.Row="1" Panel.ZIndex="100" x:Name="menu" Orientation="Horizontal" TextBlock.FontSize="18" Background="LightBlue"><TextBlock Text="首页" Margin="10"></TextBlock><TextBlock Text="编辑" Margin="10"></TextBlock><TextBlock Text="视图" Margin="10"></TextBlock><TextBlock Text="调试" Margin="10"></TextBlock><TextBlock Text="WPF UI" Margin="10"></TextBlock></StackPanel><Border Height="1000" Grid.Row="22"><Border.Background><LinearGradientBrush><GradientStop Color="Red" Offset="0"/><GradientStop Color="Green" Offset="0.5"/><GradientStop Color="Blue" Offset="1"/></LinearGradientBrush></Border.Background><TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="30" Foreground="White" Text="这是顶部Banner"/></Border></Grid></ScrollViewer><StackPanel x:Name="topMenu" VerticalAlignment="Top" Visibility="Hidden" Orientation="Horizontal" TextBlock.FontSize="18" Background="LightBlue"><TextBlock Text="首页" Margin="10"></TextBlock><TextBlock Text="编辑" Margin="10"></TextBlock><TextBlock Text="视图" Margin="10"></TextBlock><TextBlock Text="调试" Margin="10"></TextBlock><TextBlock Text="WPF UI" Margin="10"></TextBlock></StackPanel></Grid>
</Window>

MainWindow1.cs代码:

using System.Windows;namespace wpfcore
{public partial class MainWindow1 : Window{public MainWindow1(){InitializeComponent();}private void ScrollViewer_ScrollChanged(object sender, System.Windows.Controls.ScrollChangedEventArgs e){if (e.VerticalOffset > banner.ActualHeight){topMenu.Visibility = Visibility.Visible;}else{topMenu.Visibility = Visibility.Hidden;}}}
}

---------------分隔线------------------

第二种方法:

在菜单 栏内添加RenderTransform。当菜单滚动到上方时,就设置TranslateTransform.YProperty  达到 同样效果

MainWindow.xaml代码如下 :

<Window x:Class="wpfcore.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:wpfcore" xmlns:controls="http://metro.mahapps.com/winfx/xaml/controls"mc:Ignorable="d"UseLayoutRounding="True"Title="MainWindow" Width="600" Height="340"><Grid><ScrollViewer ScrollChanged="ScrollViewer_ScrollChanged"><Grid><Grid.RowDefinitions><RowDefinition Height="200"/><RowDefinition Height="auto"/><RowDefinition Height="*"/></Grid.RowDefinitions><Grid Grid.Row="0" x:Name="banner"><Image Source="D:\bizhi\国漫\2-9.jpg" Stretch="UniformToFill" HorizontalAlignment="Center" VerticalAlignment="Center"/><TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="30" Foreground="White" Text="这是顶部Banner"/></Grid><StackPanel Grid.Row="1" Panel.ZIndex="100" x:Name="menu" Orientation="Horizontal" TextBlock.FontSize="18" Background="LightBlue"><StackPanel.RenderTransform><TranslateTransform x:Name="menuTranslate" Y="0.0"/></StackPanel.RenderTransform><TextBlock Text="首页" Margin="10"></TextBlock><TextBlock Text="编辑" Margin="10"></TextBlock><TextBlock Text="视图" Margin="10"></TextBlock><TextBlock Text="调试" Margin="10"></TextBlock><TextBlock Text="WPF UI" Margin="10"></TextBlock></StackPanel><Border Height="1000" Grid.Row="22"><Border.Background><LinearGradientBrush><GradientStop Color="Red" Offset="0"/><GradientStop Color="Green" Offset="0.5"/><GradientStop Color="Blue" Offset="1"/></LinearGradientBrush></Border.Background><TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="30" Foreground="White" Text="这是顶部Banner"/></Border></Grid></ScrollViewer><StackPanel x:Name="topMenu" VerticalAlignment="Top" Visibility="Hidden" Orientation="Horizontal" TextBlock.FontSize="18" Background="LightBlue"><TextBlock Text="首页" Margin="10"></TextBlock><TextBlock Text="编辑" Margin="10"></TextBlock><TextBlock Text="视图" Margin="10"></TextBlock><TextBlock Text="调试" Margin="10"></TextBlock><TextBlock Text="WPF UI" Margin="10"></TextBlock></StackPanel></Grid>
</Window>

MainWindow.cs代码:

using System.Windows;
using System.Windows.Media;namespace wpfcore
{public partial class MainWindow : Window{public MainWindow(){InitializeComponent();}private void ScrollViewer_ScrollChanged(object sender, System.Windows.Controls.ScrollChangedEventArgs e){menuTranslate.SetValue(TranslateTransform.YProperty, e.VerticalOffset);if (e.VerticalOffset > banner.ActualHeight){menuTranslate.SetValue(TranslateTransform.YProperty, e.VerticalOffset-banner.ActualHeight);}else{menuTranslate.SetValue(TranslateTransform.YProperty, 0.0);}}}
}

你还有更好的方法吗?

欢迎加入WPF UI交流群哦,关注本公众号后,回复加群,即可获取群二维码。

如果喜欢,点个赞呗~

WPF 菜单栏滚动到顶部后固定的两种方法相关推荐

  1. DIV滚动条自动滚动到最底部的两种方法

    查看原文 方法1 function updateScroll(){var element = document.getElementById("divId");element.sc ...

  2. WPF TextBox限制只能输入数字的两种方法

    文本框中只能输入数字,一个常见的功能喽,今天就来看看如何实现它~ 下面就看看代码 思路都写在xaml里面了, MainWindow.xaml: <Window x:Class="wpf ...

  3. WPF程序将DLL嵌入到EXE的两种方法

    2019独角兽企业重金招聘Python工程师标准>>> WPF程序将DLL嵌入到EXE的两种方法 这一篇可以看作是<Visual Studio 版本转换工具WPF版开源了> ...

  4. WPF多线程UI更新——两种方法

    WPF多线程UI更新--两种方法 前言 在WPF中,在使用多线程在后台进行计算限制的异步操作的时候,如果在后台线程中对UI进行了修改,则会出现一个错误:(调用线程无法访问此对象,因为另一个线程拥有该对 ...

  5. iOS 两种方法实现左右滑动出现侧边菜单栏 slide view

        现在很多的APP中都有slide view,左右滑动出现侧边菜单栏的功能,Weico这个应用就有. 网上有很多第三方的类库实现了这种效果,其实自己代码写的话也是很简单的,下面我将介绍两种方法实 ...

  6. WPF中在XAML中实现数据类型转换的两种方法

    WPF中在XAML中实现数据类型转换的两种方法 原文:WPF中在XAML中实现数据类型转换的两种方法 熟悉数据绑定的朋友都知道,当我们在Model中获取一个对象的数据,常常需要对其进行数据转换后显示在 ...

  7. C# WPF 调用打印机的两种方法

    C# WPF 调用打印机的两种方法 最近在调试打印机,为了方便测试写了一个小demo.为了更好的判断是打印机硬件的问题还是动态库的问题,设定了定时器不间断打印来进行测试.现来分享记录一下. 需要调用两 ...

  8. mysql php gpl_MySQL_MySQL数据库远程访问权限如何打开(两种方法),下载GPL版本安装MySQL Community - phpStudy...

    MySQL数据库远程访问权限如何打开(两种方法) 下载GPL版本安装 MySQL Community Edition(GPL) Community (GPL) Downloads » 在我们使用mys ...

  9. C# 系统应用之无标题窗体移动的两种方法

    在做项目界面设计中,常常为了美观需要设置窗体属性"FormBorderStyle"(窗体边框和标题栏外观)为None无标题窗口.此时隐藏标题的窗口怎样实现移动呢?我根据自己的项目从 ...

最新文章

  1. Spring Boot中使用JavaMailSender发送邮件
  2. TypeScript入门(三)面向对象特性
  3. linux 卸载aria2,Linux Mint 19下安装aria2的过程完整总结
  4. .html(),.text()和.val()的差异总结
  5. 动态加载和静态加载及其编译步骤
  6. C#位运算讲解与示例
  7. 【Elasticsearch】elasticsearch 段 segment 段合并
  8. [教官] 目标中关村!偶滴工作日记
  9. IOS第五天(2:用户登录,回车的监听(代理模式UITextFieldDelegate)) 和关闭键盘
  10. SDK中利用COM打印helloworld时报错DONE pin is not high on target FPGA的一种可能的解决办法
  11. Postgresql数据库体系结构-存储结构
  12. 隧道管廊UWB定位系统解决方案
  13. 露出真容,小米家用摄像头拆解,看看有什么
  14. 使用三防漆的安全说明
  15. php msg oob,一个例子说明 MSG_OOB MSG_PEEK MSG_DONTWAIT
  16. 从LSM-Tree、COLA-Tree谈到StackOverflow、OSQA
  17. HTML入门学习笔记+详细案例
  18. 基础入门-算法逆向散列对称非对称JS源码逆向AESDESRSASHA
  19. 8.String str=“i“与 String str=new String(“i”)一样吗?
  20. STM32CubeMX实战TFT_LCD液晶显示-探索者专用(转发)

热门文章

  1. linux下无法umount移动设备
  2. Android开发者指南(29) —— USB Host and Accessory
  3. Redhat Linux编译安装LAMP环境
  4. 用树莓派和PC机搭建多节点私人以太坊网络
  5. FPGA浮点数定点化
  6. 求指教、。。。关于调用so文件
  7. Teams Bot开发系列:Middleware
  8. 如何在Android TV上自定义推荐行
  9. ASP.NET Session的七点认识(转)
  10. PostgreSQL忘记输入where条件update更新整张表的解决办法