开发者

How to change BitmapEffect in custom WPF control via C# code

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&g开发者_开发知识库t;

    <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
}


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜