style trigger on mouse double click to show/hide in wpf
我在这里有一个简单的项目。如何使红点开始不可见,然后当双击列表项时,单击项目的红点在 1 秒内变为可见,然后在 1 秒内淡出为 0。我查看了样式触发器,但不清楚如何开始执行此操作。
或者如下所述,如果有人可以展示如何添加一个在双击事件上运行的简单动画
MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | <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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | 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)); } } } } |
根据
对于动画,这是一个示例:
XAML:
1 | <Ellipse Name="ball" Fill="Red" Width="30" Height="30" Opacity="0" /> |
代码隐藏:
1 2 3 4 5 6 7 | 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 方式,但我个人更喜欢代码隐藏。