WPF Behavior ListBox选中项类似鱼眼效果

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Media.Animation;
using System.Windows;
using System.Windows.Interactivity;
namespace WpfBehaviorStudy.Behaviors
{
    public class SelectedItemFillBehavior : Behavior<ListBox>//把行为附加到ListBox上。ListBox被附加了下面的行为
    {
        // Using a DependencyProperty as the backing store for DefaultHeight.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DefaultHeightProperty =
            DependencyProperty.Register("DefaultHeight", typeof(double), typeof(SelectedItemFillBehavior), new UIPropertyMetadata(30.0));
        // Using a DependencyProperty as the backing store for AnimationDuration.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty AnimationDurationProperty =
            DependencyProperty.Register("AnimationDuration", typeof(int), typeof(SelectedItemFillBehavior), new UIPropertyMetadata(300));
        public double DefaultHeight
        {
            get { return (double)GetValue(DefaultHeightProperty); }
            set { SetValue(DefaultHeightProperty, value); }
        }
        public int AnimationDuration
        {
            get { return (int)GetValue(AnimationDurationProperty); }
            set { SetValue(AnimationDurationProperty, value); }
        }
        protected override void OnAttached()
        {
            base.OnAttached();
            //附加行为后需要处理的事件
            AssociatedObject.SelectionChanged += new SelectionChangedEventHandler(OnAssociatedObjectSelectionChanged);
        }
        protected override void OnDetaching()
        {
            base.OnDetaching();
            //解除的事件
            AssociatedObject.SelectionChanged -= new SelectionChangedEventHandler(OnAssociatedObjectSelectionChanged);
        }
        private void OnAssociatedObjectSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            double selectedItemFinalHeight = AssociatedObject.ActualHeight;
            Storyboard storyBoard = new Storyboard();
            for (int i = 0; i < AssociatedObject.Items.Count; i++)
            {
                ListBoxItem item = AssociatedObject.ItemContainerGenerator.ContainerFromIndex(i) as ListBoxItem;
                if (!item.IsSelected)
                {
                    selectedItemFinalHeight -= DefaultHeight;
                    DoubleAnimation heightAnimation = new DoubleAnimation()
                    {
                        To = DefaultHeight,
                        Duration = new Duration(new TimeSpan(0, 0, 0, 0, AnimationDuration))
                    };
                    Storyboard.SetTarget(heightAnimation, item);
                    Storyboard.SetTargetProperty(heightAnimation, new PropertyPath(FrameworkElement.HeightProperty));
                    storyBoard.Children.Add(heightAnimation);
                }
            }
            selectedItemFinalHeight -= 4;
            if (AssociatedObject.SelectedIndex >= 0)
            {
                ListBoxItem selectedItem = AssociatedObject.ItemContainerGenerator.ContainerFromIndex(AssociatedObject.SelectedIndex) as ListBoxItem;
                DoubleAnimation fillheightAnimation = new DoubleAnimation()
                {
                    To = selectedItemFinalHeight,
                    Duration = new Duration(new TimeSpan(0, 0, 0, 0, AnimationDuration))
                };
                Storyboard.SetTarget(fillheightAnimation, selectedItem);
                Storyboard.SetTargetProperty(fillheightAnimation, new PropertyPath(FrameworkElement.HeightProperty));
                storyBoard.Children.Add(fillheightAnimation);
            }
            storyBoard.Begin(AssociatedObject);
        }
    }
}

<Window x:Class="WpfBehaviorStudy.SelectedItemFillBehaviorWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
        xmlns:behaviors="clr-namespace:WpfBehaviorStudy.Behaviors"
        Title="SelectedItemFillBehaviorWindow" Height="300" Width="300">
    <Grid>
        <ListBox>
            <ListBoxItem Height="45">123456</ListBoxItem>
            <ListBoxItem Height="45">123456</ListBoxItem>
            <ListBoxItem Height="45">123456</ListBoxItem>
            <ListBoxItem Height="45">123456</ListBoxItem>
            <ListBoxItem Height="45">123456</ListBoxItem>
            <i:Interaction.Behaviors>
                <behaviors:SelectedItemFillBehavior />
            </i:Interaction.Behaviors>
        </ListBox>
    </Grid>
</Window>

点击查看原图

知识共享许可协议
《WPF Behavior ListBox选中项类似鱼眼效果》常伟华 创作。
采用 知识共享 署名-相同方式共享 3.0 中国大陆 许可协议进行许可。
  • 多说评论
  • 签名
  • 新浪微博
  • 默认评论
  • Tab Header 5

0 条评论 / 点击此处发表评论

Tab Content 5

Warning: file_get_contents(http://ip.taobao.com/service/getIpInfo.php?ip=34.205.93.2) [function.file-get-contents]: failed to open stream: ӷһʱûȷ𸴻ӵûзӦӳʧܡ in D:\wwwroot\changweihua\wwwroot\v\content\templates\darklight\vrecord.php on line 129

开发技术


开发平台和工具

sitemap     21049.88ms