鼠标双击样式触发器在wpf中显示/隐藏

时间:2016-01-07 20:47:59

标签: c# wpf

我这里有一个简单的项目。如何制作红点,开始不可见,然后当listitem被双击时,点击项目的RedDot在1秒的长度内变为可见,然后在1秒的长度内淡出为0。我已经查找了样式触发器,并且不清楚如何开始这样做。

或如下所述如果有人可以展示如何添加一个在doubleclick事件上运行的简单动画

enter image description here

MainWindow.xaml

<Window x:Class="DoubleExample.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:DoubleExample"
        mc:Ignorable="d"
        Title="MainWindow" Height="200" Width="200">

    <Window.DataContext>
        <local:MainWindowViewModel/>
    </Window.DataContext>

    <Grid>
        <ListView ItemsSource="{Binding People}">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Name" Width="200">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="*"/>
                                        <ColumnDefinition Width="*"/>
                                    </Grid.ColumnDefinitions>
                                    <TextBlock Grid.Column="0" Text="{Binding Name}" Foreground="Blue" Cursor="Hand" />
                                    <Ellipse Grid.Column="1" Width="10" HorizontalAlignment="Right" Height="10" Fill="Red" Margin="20,0,0,0"  VerticalAlignment="Center"/>
                                </Grid>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>
</Window>

MainWindowViewModel.cs

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace DoubleExample
{
    public class MainWindowViewModel : NotifyBase
    {
        private ObservableCollection<Person> people;
        public ObservableCollection<Person> People
        {
            get { return people; }
            set
            {
                people = value;
                NotifyPropertyChanged("People");
            }
        }


        public MainWindowViewModel()
        {
            People = new ObservableCollection<Person>();

            People.Add(new Person() { Name = "Kim" });
            People.Add(new Person() { Name = "Candy" });
            People.Add(new Person() { Name = "Doug" });

            Console.WriteLine(People.Count);
        }
    }

    public class Person
    {
        public string Name { get; set; }
    }

    public class NotifyBase : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public void NotifyPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

根据DoubleClick,似乎并不是内置的处理程序。 This回答提供了有关如何将DoubleClick绑定到ListItem的详细信息。 对于动画,这是一个例子:

XAML:

<Ellipse Name="ball" Fill="Red" Width="30" Height="30" Opacity="0" />

代码隐藏:

System.Windows.Media.Animation.DoubleAnimation animation = new System.Windows.Media.Animation.DoubleAnimation();
animation.To = 1;
animation.Duration = TimeSpan.FromSeconds(1);
animation.AccelerationRatio = 0.25;
animation.DecelerationRatio = 0.25;
animation.AutoReverse = true;
ball.BeginAnimation(Ellipse.OpacityProperty, animation);

将它放在事件处理程序中。这会在红球上产生一次闪光。还有一种全XAML方式,但我个人更喜欢代码隐藏。