我有一个有编程方式添加的动态图像数量的堆叠面板,有没有一种方法可以在这些图像上以编程方式设置悬停/点击效果。我希望图片在点击时“发光”。我如何在Silverlight中做到这一点?我注意到了Image.Effect
属性,但我不确定如何使用它。从代码隐藏中添加图片silverlight hovereffect
3
A
回答
2
你需要做的是创建一个新的用户控件,其中的图像控件与视觉状态连接在一起。这样,您可以动态地将usercontrol添加到堆栈面板,并调用动画,而不必通过主应用中的事件附加它们。
我在Image.Effect
上使用DropShadowEffect
创建了一个脉动动画。
例如,这是你的用户控件中:
XAML
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="ImageState">
<VisualState x:Name="NormalImageState">
<Storyboard>
<DoubleAnimation Duration="0" To="0" Storyboard.TargetProperty="(UIElement.Effect).(DropShadowEffect.BlurRadius)" Storyboard.TargetName="image1" d:IsOptimized="True"/>
</Storyboard>
</VisualState>
<VisualState x:Name="GlowingImageState">
<Storyboard AutoReverse="True">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Effect).(DropShadowEffect.BlurRadius)" Storyboard.TargetName="image1">
<EasingDoubleKeyFrame KeyTime="0" Value="0"/>
<EasingDoubleKeyFrame KeyTime="0:0:1" Value="20"/>
<EasingDoubleKeyFrame KeyTime="0:0:2" Value="0"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Image Name="image1" MouseEnter="image1_MouseEnter" MouseLeave="image1_MouseLeave" >
<Image.Effect>
<DropShadowEffect Color="Red" BlurRadius="0" ShadowDepth="0"/>
</Image.Effect>
</Image>
C#
public ImageSource ImageSource
{
get;
set
{
image1.Source = value;
}
}
private void image1_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
VisualStateManager.GoToState(this, "GlowingImageState", true);
}
private void image1_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
{
VisualStateManager.GoToState(this, "NormalImageState", true);
}
然后你就可以在这个用户控件添加到您的堆叠面板像这样:
MyUC uc= new MyUC(); //control we just created
uc.ImageSource = sourceOfImg; //the source of the intended image
myStack.Children.Add(uc); //adding it to the stackpanel.
告诉我我这个工作。
1
您可以使用转换来创建动画,以便在点击图像时更改图像的颜色。
看看MSDN页面:Animation Overview。本页面包含有关如何以编程方式执行此操作的详细信息(Creating an Animation in Procedural Code)。
+0
感谢您的建议 – Jakob 2010-05-30 22:24:53
相关问题
- 1. 添加代码隐藏从C#Asp.net
- 2. 在代码隐藏中添加ListBoxItems
- 3. 在silverlight中添加图片
- 4. 为图片添加隐藏值
- 5. 设置Bing地图Silverlight控件属性从代码隐藏
- 6. 如何在Silverlight的代码中为图片添加png?
- 7. 从视图中的代码隐藏
- 8. 从代码隐藏
- 9. 从代码隐藏
- 10. 从代码隐藏
- 11. 从代码隐藏
- 12. 从代码隐藏
- 13. 从代码隐藏
- 14. 从代码隐藏
- 15. Datagrid中的工具提示,从代码隐藏中添加
- 16. 添加RotateTransform动画代码隐藏
- 17. WPF DataGrid行添加代码隐藏
- 18. 添加OnClick到代码隐藏按钮
- 19. 添加asp:代码隐藏按钮
- 20. Silverlight helix代码隐藏页面导航
- 21. 的Silverlight XAML:引用代码隐藏类
- 22. 如何在代码隐藏中将图像添加到Rss?
- 23. 从代码隐藏中将类添加到listitem
- 24. 从代码隐藏中向网页添加文本
- 25. 从代码隐藏添加ascx时,ascx中的控件为空
- 26. 从代码隐藏的SqlDataSource中添加参数
- 27. 从C#代码隐藏在ext.net中添加新窗口
- 28. 从代码隐藏添加资源并在XAML中使用
- 29. 从代码隐藏中添加<asp:image>
- 30. 如何从代码隐藏添加到页面的html中?
感谢您的建议。我去了,实际上改变了ffecttype,但用户控制方法是我拿的! – Jakob 2010-05-30 22:24:16