使用触发器向 UIElement 动态添加发光效果
我是 wpf 新手,正在寻找好的教程来帮助更好地理解触发器,但我运气不佳。所以我想我应该在这里寻求一些帮助。这就是我想要做的,我有一个带有堆栈面板的 ScrollViewer,在后面的代码中我浏览媒体文件夹并使用 foreach 循环将 MediaElements 添加到堆栈面板,我想做的是当用户将鼠标悬停在一个其中,我希望它在它下面发光,我被告知触发器是要走的路,所以这就是我到目前为止所看到的
foreach
MediaElement newVideoPreview = new MediaElement();
newVideoPreview.Width = 125;
newVideoPreview.Stretch = Stretch.Uniform;
newVideoPreview.Margin = new Thickness(5, 5, 5, 5);
newVideoPreview.Volume = 0;
Trigger trig = new Trigger();
trig.Property = IsMouseOverProperty;
trig.Value = true;
Style style = new Style();
style.TargetType = newVideoPreview.GetType();
style.Triggers.Add(trig);
Setter set = new Setter();
OuterGlowBitmapEffect glow = new OuterGlowBitmapEffect();
glow.GlowColor = Color.FromRgb(0, 0, 205);
glow.GlowSize = 10;
set.Value = glow; // <- Crash house
set.Property = EffectProperty;
style.Setters.Add(set);
newVideoPreview.Style = style;
,当我尝试设置设置器时,我收到无效参数异常。值,我正在寻找有关如何解决此问题或做得更好或更好的教程的建议...感谢您的任何帮助
p.s 我正在使用 VS2010 beta 2
更新我也尝试过这个工作...
<UserControl x:Class="WiiDSUKiosk.WiiFriendlyScrollViewer"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<Grid.Resources>
<Style TargetType="{x:Type UIElement}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="BitmapEffect">
<Setter.Value>
<OuterGlowBitmapEffect GlowColor="Navy" GlowSize="10"/>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</Grid.Resources>
<ScrollViewer Name="wiiFriendlyScrollViewer" ScrollViewer.HorizontalScrollBarVisibility="Hidden" ScrollViewer.VerticalScrollBarVisibility="Hidden"
MouseMove="wiiFriendlyScrollViewer_MouseMove" >
<StackPanel Name="stackPanelContent" ScrollViewer.HorizontalScrollBarVisibility="Hidden" ScrollViewer.VerticalScrollBarVisibility="Hidden">
</StackPanel>
</ScrollViewer>
</Grid>
Im new to wpf, and looking for good tutorials to help understand triggers better but im not having much luck. So i thought I would seek some help here. Here is what im trying to do, i have a ScrollViewer that has a stack panel, in the code behind I browse a media folder and added MediaElements to the stackpanel using a foreach loop, what I want to do is when a user hovers over one of these, i want it to glow underneath it, I'm told triggers are the way to go, so here is what i have so far
foreach
MediaElement newVideoPreview = new MediaElement();
newVideoPreview.Width = 125;
newVideoPreview.Stretch = Stretch.Uniform;
newVideoPreview.Margin = new Thickness(5, 5, 5, 5);
newVideoPreview.Volume = 0;
Trigger trig = new Trigger();
trig.Property = IsMouseOverProperty;
trig.Value = true;
Style style = new Style();
style.TargetType = newVideoPreview.GetType();
style.Triggers.Add(trig);
Setter set = new Setter();
OuterGlowBitmapEffect glow = new OuterGlowBitmapEffect();
glow.GlowColor = Color.FromRgb(0, 0, 205);
glow.GlowSize = 10;
set.Value = glow; // <- Crash house
set.Property = EffectProperty;
style.Setters.Add(set);
newVideoPreview.Style = style;
as you can see, i get an invalid argument exception when i try to set the setter.value, im looking for suggestions on how to fix this or do it better, or better tutorials...Thanks for any help
p.s I am using VS2010 beta 2
Update Ive tried this too this dosent work....
<UserControl x:Class="WiiDSUKiosk.WiiFriendlyScrollViewer"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<Grid.Resources>
<Style TargetType="{x:Type UIElement}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="BitmapEffect">
<Setter.Value>
<OuterGlowBitmapEffect GlowColor="Navy" GlowSize="10"/>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</Grid.Resources>
<ScrollViewer Name="wiiFriendlyScrollViewer" ScrollViewer.HorizontalScrollBarVisibility="Hidden" ScrollViewer.VerticalScrollBarVisibility="Hidden"
MouseMove="wiiFriendlyScrollViewer_MouseMove" >
<StackPanel Name="stackPanelContent" ScrollViewer.HorizontalScrollBarVisibility="Hidden" ScrollViewer.VerticalScrollBarVisibility="Hidden">
</StackPanel>
</ScrollViewer>
</Grid>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我也只是在这个问题上花了几个小时,只是为了发现 bitmapeffects 在 vs 2010 中已经过时了。这个答案基于这篇文章:msdn 论坛
I just spent a few hours on this one too, just to find out that bitmapeffects has become obsolete in vs 2010. Basing this answer on this post : msdn forums
这在 xaml 中更容易做到,尝试在代码中操作这些东西是一件令人头痛的事情。
这篇不相关的文章中有一些代码< /a> 关于项目控制生成器,当在列表框中选择项目时,该生成器将添加发光。 (向下一半多一点)
这里是 ms例如它使用触发器来发光任何具有焦点的东西。 (这样更容易理解)
this is a lot easier to do in xaml, trying to manipulate this stuff in code is a headache.
there is some code in this unrelated article about items control generators that will add the glow when an item is selected in a list box. ( a little more than half way down )
here is the ms example it uses triggers to glow anything that has focus. ( it is a lot easier to understand )