idish
idish

Reputation: 3260

Setting opacity of a rectangle

I have an event fired when some button is being clicked

private void set_Opacity(object sender, RoutedEventArgs e)
    {
        this.Opacity = 0;
    }

But there's no effect when the button is clicked. What am I doing wrong? Thanks.

EDIT : I'll give some background about what I am trying to do:

I have created a customized button that should minimize my window with a fade out animation so here's the code of it:

 private void minimize_Window(object sender, EventArgs e)
    {
        var anim = new DoubleAnimation(0, (Duration)TimeSpan.FromSeconds(1));
        anim.Completed += (s, _) => this.Minimize();
        this.BeginAnimation(UIElement.OpacityProperty, anim);



    }
    private void Minimize()
    {
        WindowState = WindowState.Minimized;
    }

It works perfectly fine, though the problem is that when I try to retrieve my program from the task bar it won't do anything (I am not able to see the program when I click on it from the taskbar after it minimized). What I understand from it is that the opacity of my program is being set to 0 while it got minimized(because of the animation). So I used the Activated event calling that method:

private void set_Opacity(object sender, EventArgs e)
    {
        rectangle2.Opacity = 1;
        WindowState = WindowState.Normal;
    }

And still, same issue. Hope you could help.

Thank you very much.

Upvotes: 1

Views: 3183

Answers (2)

vcsjones
vcsjones

Reputation: 141638

The problem seems to be that in most cases, this is going to be a Window. My assumption though, is that you are not trying to set the entire window to invisible, rather a Rectangle. You need to give your Rectangle a name using x:Name attribute. Here is an example:

<Rectangle Width="40" Height="40" x:Name="MyRectangle" Fill="Red" />

Then, in your button click:

private void set_Opacity(object sender, RoutedEventArgs e)
{
    MyRectangle.Opacity = 0;
}

If you really are trying to set the entire Window's transparency:

An entire Window can't have it's opacity set unless you specify AllowTransparency to true and WindowStyle to None in your XAML:

<Window x:Class="ScratchApplication.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        AllowsTransparency="true" WindowStyle="None">

Note the attributes being set.

EDIT:

I need an event to be fired when my program is being activated back from my task bar. Do you have any idea what event I could use?

Use the Window.Activated event. From your XAML:

<Window Activated="Window_Activated">

And in your C#:

private void Window_Activated(object sender, EventArgs e)
{
    //Put code here
}

Upvotes: 4

Russell Smith
Russell Smith

Reputation: 26

This should have some effect (assuming this is being called on a WPF applications main window it will make the whole window go black).

It almost sounds like the event handler isn't correctly wired up. Try setting a break point on the opacity setting line or right clicking on the set_opacity function name and selection 'Find All References', you should see an event handler being created in the windowname.g.cs file.

Upvotes: 0

Related Questions