views:

452

answers:

2

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
}
+2  A: 
myGrid.BitmapEffect = null;

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:

<Window x:Class="WpfCsApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">

<Grid Background="#FFE5AA" Margin="10" MouseDown="Grid_MouseDown" x:Name="myGrid">
    <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" >
        <TextBlock>Test</TextBlock>
    </Border>
</Grid>
</Window>

Codebehind:

public partial class Window1 : Window {
    public Window1() {
        InitializeComponent();
    }

    private void Grid_MouseDown(object sender, MouseButtonEventArgs e) {
        myGrid.BitmapEffect = null;
    }
}


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.

Heinzi
It doesn't work.
Kamilos
I just tried it and it works fine. Can you provide a short example to reproduce the problem?
Heinzi
Ok, look above.
Kamilos
Working sample added.
Heinzi
Your sample working for me too. But it not woeking in my app :|Btw. can You add an effect in C# code? I need glow grid onClick and disable glow in other grids, etc...
Kamilos
+1  A: 

OK, I've got it! I was add an DepencyProperty 'GlowSize' and simply change size of glow via it. :) Works perfect.

Kamilos