如何通过 C# 代码更改自定义 WPF 控件中的 BitmapEffect
我有一个自定义控件类型,例如:
和 Grid.BitmapEffect 属性。如何通过 C# 代码(例如在事件上)更改此控件(网格)中的 BitmapEffetc?
代码示例 - 自定义控件的一部分:
[...]
<Grid Background="#FFE5AA">
<Grid.RowDefinitions>
<RowDefinition Height="62*"/>
<RowDefinition Height="15*"/>
<RowDefinition Height="23*"/>
</Grid.RowDefinitions>
<Grid.BitmapEffect>
<OuterGlowBitmapEffect GlowColor="#459E5A" GlowSize="13" Noise="0" Opacity="0.9" />
</Grid.BitmapEffect>
<Border Grid.Column="0" Grid.Row="0" Grid.RowSpan="3" BorderBrush="#F5B903" BorderThickness="1,1,1,1" >
</Border>
[...]
然后在 Window.xaml 中:
<controls:MyControl Name="Control1" Cursor="Hand" MouseDown="Control1_MouseDown" />
然后在 C# 中:
private void Control1_MouseDown(object sender, MouseButtonEventArgs e)
{
//there i want to change Control1.BitmapEffect
}
I have a custom control type like: <Grid> ... </Grid>
and Grid.BitmapEffect property. How can I change BitmapEffetc in this Control (Grid) via C# code (e.g. on event)?
Code sample - part of custom control:
[...]
<Grid Background="#FFE5AA">
<Grid.RowDefinitions>
<RowDefinition Height="62*"/>
<RowDefinition Height="15*"/>
<RowDefinition Height="23*"/>
</Grid.RowDefinitions>
<Grid.BitmapEffect>
<OuterGlowBitmapEffect GlowColor="#459E5A" GlowSize="13" Noise="0" Opacity="0.9" />
</Grid.BitmapEffect>
<Border Grid.Column="0" Grid.Row="0" Grid.RowSpan="3" BorderBrush="#F5B903" BorderThickness="1,1,1,1" >
</Border>
[...]
Then in Window.xaml:
<controls:MyControl Name="Control1" Cursor="Hand" MouseDown="Control1_MouseDown" />
Then in C#:
private void Control1_MouseDown(object sender, MouseButtonEventArgs e)
{
//there i want to change Control1.BitmapEffect
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
PS:请注意, BitmapEffect 被认为已过时并且应该使用 Effect 。
这是一个基于您的示例的示例,该示例运行得很好(在我的机器上):只要我在网格内单击,效果就会消失。
XAML:
代码隐藏:
在您的示例中,您编写:
//我想更改 Control1.BitmapEffect
。请注意,您需要更改 Grid 的 BitmapEffect,而不是 Control1 的 BitmapEffect。PS: Note that BitmapEffect is considered obsolete and that Effect should be used instead.
Here's an example based on your sample which works perfectly fine (here on my machine): As soon as I click within the Grid, the effect disappears.
XAML:
Codebehind:
In your example you write:
//there i want to change Control1.BitmapEffect
. Note that you need to change the BitmapEffect of the Grid, not the BitmapEffect of Control1.好的,我知道了!我添加了一个 DepencyProperty 'GlowSize' 并通过它简单地更改发光的大小。 :) 工作完美。
OK, I've got it! I was add an DepencyProperty 'GlowSize' and simply change size of glow via it. :) Works perfect.
这里列出了不同的效果:不同的位图效果
Different effects are listed here : Different BitmapEffect