在 C# 中,如何在图像上动态创建触发器
In C# how can I dynamically create trigger on an image
我正在尝试动态创建当您将鼠标悬停在图像上时会放大的图像。我发现这个博客 post http://tozon.info/blog/post/2007/10/14/Three-ways-to-make-your-WPF-images-pop-out-on-MouseOver.aspx 展示了如何使用情节提要来执行此操作。
它看起来很简单,我已经将 StoryBoard xaml 添加到 App.xaml
现在我需要研究如何将它们添加到我用 C# 构建的图像中
必须创建的 xaml 如下所示
<Image Source="[Bitmap]" >
<Image.Triggers>
<EventTrigger RoutedEvent="Image.MouseEnter" >
<BeginStoryboard Storyboard="{StaticResource ExpandStoryboard}" />
</EventTrigger>
<EventTrigger RoutedEvent="Image.MouseLeave" >
<BeginStoryboard Storyboard="{StaticResource ShrinkStoryboard}" />
</EventTrigger>
</Image.Triggers>
<Image.RenderTransform>
<ScaleTransform ScaleX="1" ScaleY="1" />
</Image.RenderTransform>
</Image>
我已经在 C# 中创建了图像、故事板和 RenderTransform(我认为),但我不确定如何将故事板添加为 EventTriggers。
System.Windows.Controls.Image _Image = new System.Windows.Controls.Image();
_Image.Source = this.BitmapToBitmapImage(_Bitmap); // This bit is not really relevant for this question. It works fine though.
ScaleTransform _OriginalScale = new ScaleTransform();
_OriginalScale.ScaleX = 1;
_OriginalScale.ScaleY = 1;
_Image.RenderTransform = _OriginalScale;
Storyboard _ExpandStory = (Storyboard)Application.Current.FindResource("ExpandStoryboard");
Storyboard _ShrinkStory = (Storyboard)Application.Current.FindResource("ShrinkStoryboard");
_Image.Triggers.Add(WHAT GOES HERE);
您可以再创建 2 个 EventTrigger
并正常添加到 Triggers
。
var e1 = new EventTrigger(UIElement.MouseEnterEvent);
//add actions for e1
e1.Actions.Add(new BeginStoryboard{ Storyboard = _ExpandStory});
var e2 = new EventTrigger(UIElement.MouseLeaveEvent);
//add actions for e2
e2.Actions.Add(new BeginStoryboard { Storyboard = _ShrinkStory});
//add the 2 event triggers
_Image.Triggers.Add(e1);
_Image.Triggers.Add(e2);
我正在尝试动态创建当您将鼠标悬停在图像上时会放大的图像。我发现这个博客 post http://tozon.info/blog/post/2007/10/14/Three-ways-to-make-your-WPF-images-pop-out-on-MouseOver.aspx 展示了如何使用情节提要来执行此操作。
它看起来很简单,我已经将 StoryBoard xaml 添加到 App.xaml
现在我需要研究如何将它们添加到我用 C# 构建的图像中
必须创建的 xaml 如下所示
<Image Source="[Bitmap]" >
<Image.Triggers>
<EventTrigger RoutedEvent="Image.MouseEnter" >
<BeginStoryboard Storyboard="{StaticResource ExpandStoryboard}" />
</EventTrigger>
<EventTrigger RoutedEvent="Image.MouseLeave" >
<BeginStoryboard Storyboard="{StaticResource ShrinkStoryboard}" />
</EventTrigger>
</Image.Triggers>
<Image.RenderTransform>
<ScaleTransform ScaleX="1" ScaleY="1" />
</Image.RenderTransform>
</Image>
我已经在 C# 中创建了图像、故事板和 RenderTransform(我认为),但我不确定如何将故事板添加为 EventTriggers。
System.Windows.Controls.Image _Image = new System.Windows.Controls.Image();
_Image.Source = this.BitmapToBitmapImage(_Bitmap); // This bit is not really relevant for this question. It works fine though.
ScaleTransform _OriginalScale = new ScaleTransform();
_OriginalScale.ScaleX = 1;
_OriginalScale.ScaleY = 1;
_Image.RenderTransform = _OriginalScale;
Storyboard _ExpandStory = (Storyboard)Application.Current.FindResource("ExpandStoryboard");
Storyboard _ShrinkStory = (Storyboard)Application.Current.FindResource("ShrinkStoryboard");
_Image.Triggers.Add(WHAT GOES HERE);
您可以再创建 2 个 EventTrigger
并正常添加到 Triggers
。
var e1 = new EventTrigger(UIElement.MouseEnterEvent);
//add actions for e1
e1.Actions.Add(new BeginStoryboard{ Storyboard = _ExpandStory});
var e2 = new EventTrigger(UIElement.MouseLeaveEvent);
//add actions for e2
e2.Actions.Add(new BeginStoryboard { Storyboard = _ShrinkStory});
//add the 2 event triggers
_Image.Triggers.Add(e1);
_Image.Triggers.Add(e2);