wpf listbox实现选中动画

效果如下:

参考:https://github.com/WPFDevelopersOrg/WPFDevelopers/blob/master/src/WPFDevelopers.Samples.Shared/Controls/NavigateMenu/NavigateMenu.xaml

实现上述效果的前台代码:

bash 复制代码
<Window
    x:Class="ListBox.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:local="clr-namespace:ListBox"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Title="MainWindow"
    Width="800"
    Height="450"
    mc:Ignorable="d">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <ListBox x:Name="listbox">
            <ListBoxItem>1</ListBoxItem>
            <ListBoxItem>2</ListBoxItem>
            <ListBoxItem>3</ListBoxItem>
            <ListBoxItem>4</ListBoxItem>
            <ListBoxItem>5</ListBoxItem>
        </ListBox>
        <Rectangle
            x:Name="rect"
            Width="2"
            Height="20"
            HorizontalAlignment="Left"
            VerticalAlignment="Top"
            Fill="Red">
           
        </Rectangle>
    </Grid>
</Window>

后台代码:

csharp 复制代码
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.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ListBox
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.listbox.SelectionChanged += Listbox_SelectionChanged;
        }

        private double? lastX = null;
        private double? lastY = null;
        private void Listbox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var itemTo = e.AddedItems[0] as ListBoxItem;
            var pointTo = itemTo.TranslatePoint(new Point(0, 0), this);
 
            var toMargin = new Thickness(pointTo.X, pointTo.Y, 0, 0);
            ThicknessAnimation ani = new ThicknessAnimation();
            ani.To = toMargin;
            ani.Duration = TimeSpan.FromSeconds(0.3);
            this.rect.BeginAnimation(Rectangle.MarginProperty, ani);
            ani.Completed += (ss, ee) =>
            {
                this.rect.Margin = toMargin;
            };
        }
    }
}

同理,依据此,可以实现TabControlTabItem选中动画

相关推荐
月落.10 小时前
WPF的<ContentControl>控件
wpf
就是有点傻10 小时前
WPF中的依赖属性
开发语言·wpf
wangnaisheng10 小时前
【WPF】把一个Window放在左上角/右上角顶格显示
wpf
WineMonk10 小时前
.NET WPF CommunityToolkit.Mvvm框架
.net·wpf·mvvm
月落.10 小时前
WPF中的INotifyPropertyChanged接口
wpf
界面开发小八哥10 小时前
界面控件DevExpress WPF中文教程:Data Grid——卡片视图设置
.net·wpf·界面控件·devexpress·ui开发
平凡シンプル10 小时前
WPF 打包
wpf
VickyJames10 小时前
基于XAML框架和跨平台项目架构设计的深入技术分析
wpf·开源分享·unoplatform·winui3·项目架构
冷眼Σ(-᷅_-᷄๑)14 小时前
WPF缩放动画和平移动画叠加后会发生什么?
wpf·动画
△曉風殘月〆16 小时前
WPF MVVM入门系列教程(二、依赖属性)
c#·wpf·mvvm