当前位置: 首页 > article >正文

wpf 制作丝滑Flyout浮出侧边栏Demo (Mahapps UI框架)

在这里插入图片描述
Flyout 属性

  • CloseButtonVisibility: 设置为 Collapsed,意味着关闭按钮不可见。
  • TitleVisibility: 设置为 Collapsed,意味着标题不可见。
  • IsPinned: 设置为 True,意味着这个 Flyout 会固定住,不会自动关闭。
  • Opacity: 设置为 1,意味着完全不透明。
  • Position: 设置为 Right,意味着这个 Flyout 会出现在右侧。
  • Width: 设置为 auto,意味着宽度会根据内容自动调整。
  • Background: 背景色设置为 AliceBlue。
  • IsOpen: 设置为 False,意味着默认情况下这个 Flyout 是关闭的。
<controls:Flyout x:Class="ControlsTest.FloatingBox"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:ControlsTest"
             mc:Ignorable="d" 
             xmlns:controls="http://metro.mahapps.com/winfx/xaml/controls" 
             CloseButtonVisibility="Collapsed" TitleVisibility="Collapsed" 
             IsPinned="True" Opacity="1"
             Position="Right" Width="auto" Background="AliceBlue" IsOpen="False">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>


        <StackPanel Grid.Row="0">
            <Button Content="123" Width="50"/>
            <Button Width="50" Content="345"/>
        </StackPanel>

        <StackPanel Grid.Column="1">
            <Button Content="123" Width="30" Height="50" Click="Button_Click_1"/>
        </StackPanel>

    </Grid>
</controls:Flyout>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ControlsTest
{
    /// <summary>
    /// FloatingBox.xaml 的交互逻辑
    /// </summary>
    public partial class FloatingBox
    {
        public FloatingBox()
        {
            InitializeComponent();
        }

        public static readonly DependencyProperty ButtonClickCommandProperty =
    DependencyProperty.Register("ButtonClickCommand", typeof(ICommand), typeof(FloatingBox), new PropertyMetadata(null));

        public ICommand ButtonClickCommand
        {
            get { return (ICommand)GetValue(ButtonClickCommandProperty); }
            set { SetValue(ButtonClickCommandProperty, value); }
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            if (ButtonClickCommand != null && ButtonClickCommand.CanExecute(null))
            {
                ButtonClickCommand.Execute(null);
            }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using System.Windows;

namespace ControlsTest
{
    public class BoolToCollapsedConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            Visibility result = Visibility.Collapsed;

            if (value == null || value == DependencyProperty.UnsetValue)
            {
                return result;
            }

            string para = parameter as string;

            if (string.IsNullOrWhiteSpace(para)) 
            {
                result = (((bool)value) ? Visibility.Collapsed : Visibility.Visible);
                return result;
            }
    
            if (string.Equals(para, "reverse", StringComparison.OrdinalIgnoreCase))
            {
                result = ((!(bool)value) ? Visibility.Collapsed : Visibility.Visible);
                return result;
            }

            return result;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value;
        }
    }
}
<Window x:Class="ControlsTest.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:ControlsTest"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <StackPanel>
            <Button Width="50"/>
        </StackPanel>
        
        <Grid Grid.Row="1">
            <Button Content="Open" Height="30" Width="50" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="0,20,40,0" Click="Button_Click" Visibility="{Binding FbisOpen,Converter={StaticResource BoolToCollapsed}}" Background="Red"/>
            <local:FloatingBox IsOpen="{Binding FbisOpen,UpdateSourceTrigger=PropertyChanged}" Height="100" ButtonClickCommand="{Binding ButtonClickCommand}"/>
        </Grid>
        
    </Grid>
</Window>

using PropertyChanged;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ControlsTest
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
     [AddINotifyPropertyChangedInterface]
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;
            ButtonClickCommand = new RelayCommand(OnButtonClick);
        }

        private void OnButtonClick()
        {
            FbisOpen = false;
        }

        private bool fbisOpen = true;

        public bool FbisOpen
        {
            get { return fbisOpen; }
            set { fbisOpen = value; }
        } 

        public ICommand ButtonClickCommand { get; set; }


        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if(FbisOpen)
            {
                FbisOpen = false;
            }
            else
            {
                FbisOpen = true;
            }
        }
    }

    public class RelayCommand : ICommand
    {
        private readonly Action _execute;
        private readonly Func<bool> _canExecute;

        public RelayCommand(Action execute, Func<bool> canExecute = null)
        {
            _execute = execute ?? throw new ArgumentNullException(nameof(execute));
            _canExecute = canExecute;
        }

        public bool CanExecute(object parameter)
        {
            return _canExecute == null || _canExecute();
        }

        public void Execute(object parameter)
        {
            _execute();
        }

        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }
    }
}


http://www.kler.cn/a/384459.html

相关文章:

  • Java多线程详解⑤(全程干货!!!)线程安全问题 || 锁 || synchronized
  • RDD转换算子:【map】
  • 【网络原理】关于HTTP状态码以及请求的构造的哪些事
  • 【JAVA】第3关:素数链
  • Java版ERP管理系统源码解析:利用Spring Cloud Alibaba和Spring Boot实现微服务架构
  • Python 基础笔记之生成器generator
  • Springboot 整合 Java DL4J 打造自然语言处理之语音识别系统
  • TypeScript完整学习 --【比降价金还值!】
  • 计算机毕业设计Python+图神经网络手机推荐系统 手机价格预测 手机可视化 手机数据分析 手机爬虫 Django Flask Spark 知识图谱
  • vue3 ref对象的width改变了,并不会直接去更新视图,但是触发obj.width++是可以正常更新视图的简单处理方法
  • springBoot动态加载jar,将类注册到IOC
  • MongoDB笔记01-概念与安装
  • <网络> 协议
  • go 集成viper 配置管理
  • 简易抽奖器源码以及打包操作
  • 【网络-交换机】生成树协议、环路检测
  • Java 8 Stream API 详解
  • 实时金融股票数据API接口websocket接入方法
  • WRF-LES与PALM模型:风能资源评估、风力发电、大涡模拟、大尺度湍流涡旋、大雾预报、局地环流模拟、城市热岛效应、流场模拟
  • 香港服务器怎么搭建docker加速器
  • flutter 项目初建碰到的控制台报错无法启动问题
  • 地理空间-Java实现航迹稀释
  • 【北京迅为】《STM32MP157开发板嵌入式开发指南》-第七十二章 Debian文件系统
  • Java反射原理及其性能优化
  • C#实战:使用腾讯云识别服务轻松提取火车票信息
  • 科研绘图系列:R语言组合连线图和箱线图(linechart+boxplot)