【问题标题】:In C# how can I dynamically create trigger on an image在 C# 中,如何在图像上动态创建触发器
【发布时间】:2016-01-21 03:33:06
【问题描述】:

我正在尝试动态创建当您将鼠标悬停在它们上方时会放大的图像。我发现这篇博文 http://tozon.info/blog/post/2007/10/14/Three-ways-to-make-your-WPF-images-pop-out-on-MouseOver.aspx 展示了如何使用 Storyboards 做到这一点。

它看起来很简单,我已将 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(我认为),但我不确定如何将情节提要添加为 EventTrigger。

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);

【问题讨论】:

    标签: c# wpf storyboard event-triggers


    【解决方案1】:

    您可以再创建 2 个 EventTriggers 并正常添加到 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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-10-28
      • 1970-01-01
      • 2011-07-13
      • 1970-01-01
      • 2011-09-02
      • 1970-01-01
      • 2011-01-29
      相关资源
      最近更新 更多