WPF / XAML - Why doesn't this trigger work on a button

HI..
I have the following.. I'm trying to update a progress bar when a button is clicked. But nothing happens.. no errors.. what's wrong with this?


                    <Button Name="btnImport" Width="80" Grid.Row="2"
                                        Height="28"            
                                        Margin="25,5,10,5"
                                        HorizontalAlignment="Right" Content="Import" ToolTip="Import File "  Click="btnImport_Click">

                         <Button.Triggers>
                            <EventTrigger RoutedEvent="Button.Click">
                                <BeginStoryboard>
                                    <Storyboard>
                                        <DoubleAnimation Storyboard.TargetName="progressBar"
                                                Storyboard.TargetProperty="Value"
                                                 From="0" To="100" Duration="0:0:10" RepeatBehavior="Forever"/>
                                    </Storyboard>
                                </BeginStoryboard>
                            </EventTrigger>
                        </Button.Triggers>


                    </Button>
LVL 1
JElsterAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

 
saraganiCommented:
It works for me...
0
 
saraganiCommented:
You do have btnImport_Click...
Make sure that you don't do any: e.Handled = true;
0
 
JElsterAuthor Commented:
The progress.value never seems to change....
??????????????
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
saraganiCommented:
Do you have e.Handled =true in your click event?

Can you post your entire code? (xaml + cs)
0
 
JElsterAuthor Commented:
It's something in the click event that interfers with it....
I calll a couple of other methods..

If I remove all the code in the click it works
0
 
saraganiCommented:
If one of youf functions get the "e", it might be doing whT I mentioned...
0
 
JElsterAuthor Commented:
The other methods.. totally block the update..
0
 
saraganiCommented:
Can you upload the code to rapidshare or megaupload, so I could check it?

Yes, I understand that the functions screw the trigger, but my question is: do they handle the event args?
0
 
JElsterAuthor Commented:
Here's some of the code... this section doesn't work either... Just unzips a file

            Title = "Importing  " + txtPWRfilename.Text;
            this.Cursor = Cursors.AppStarting;  
            this.progressBar.Visibility = Visibility.Visible;
                     
            string _tempFolder = this.theDataManager.SharedData.WorkingFolder + "\\TEMP\\";

            DM.DataManager newDM = new DM.DataManager();

            try
            {
               // this.progressBar.Value = 0;
                this.lblStatus.Content = "Creating Temp Folder...";
               

                // Create TEMP folder to unzip to
                if (!Directory.Exists(_tempFolder))
                {
                    Directory.CreateDirectory(_tempFolder);
                }

                this.lblStatus.Content = "Unzipping  File...";

                // Unzip to TEMP working folder
                ZipFile zip = new ZipFile(_PWRfile);
                FastZipEvents events = new FastZipEvents();
                FastZip fastZip = new FastZip(events);  //TODO

                fastZip.CreateEmptyDirectories = true;
                fastZip.ExtractZip(_PWRfile, _tempFolder, null);
0
 
saraganiCommented:
What is the relation of this code to the button click event?
It is only a partial code. I don't know if it's a part of the click event handler, or a part of the code of one of the functions you mentioned.


"this section doesn't work either".
Doesn't work with what? This code is preventing your storyboard from starting? Or it is just not working and it is not even related to the original question??

I have nothing to do with half of code.
0
 
JElsterAuthor Commented:
This is the code in the Click event... that's it!
This code alone prevents the storyboard....   I don't see how it blocks the UI... if I comment the code out.. the progress bar works fine
thanks again
0
 
saraganiCommented:
It doesn't seem the be the whole code.

I see that in the beginning of the code you do:
this.progressBar.Visibility = Visibility.Visible;


Does it mean that the progress bar is Hidden/Collapsed before clicking the button?

Does the Progress Bar returns to be Hidden/Collapsed at the end of the Event Handler (After the Zip file was extracted)?


Was the purpose of the Story Board to show the progress of the extraction while it extracts the file??

Can I assume that the file extraction takes 10 seconds?
0
 
JElsterAuthor Commented:
Yes.. the progress bar is initially hidden , I've also tried adding a 'doevents' after the

this.progressBar.Visibility = Visibility.Visible;

And also tried just keeping the progressbar visible...  

The progress bar is not collapsed again until the form closes...

Yes the file extractions takes 10-15 seconds..

Yes the progress bar is just eye candy during the file extraction..... nothing more... don't care about the 'value'  settings..

thx
0
 
saraganiCommented:
Hi, while the file is being extracted, the UI thread is busy and therefore no animation happens.
(Your UI will also be" not responding")

Can you please commend the following line and see if the animation starts working again?
 fastZip.ExtractZip(_PWRfile, _tempFolder, null);

(Just comment that line...)
0
 
saraganiCommented:
Furthermore,
Don't do DoEvents....

I suppose your DoEvents code is like this:

        void DoEvents()
        {
            DispatcherFrame f = new DispatcherFrame();
            Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background,
            (SendOrPostCallback)delegate(object arg)
            {
                DispatcherFrame fr = arg as DispatcherFrame;
                fr.Continue = true;
            }, f);
            Dispatcher.PushFrame(f);
        }



If you call in your Click Event to DoEvents then it will prevent the animation from working.
0
 
JElsterAuthor Commented:
Yes... If I comment out the line it works
0
 
saraganiCommented:
Hi, so first of all, don't use DoEvents.
It was good for VB6, but on .Net you have threads so if some process takes more than 0.5 second it is suggested to do that task in a different thread in order to keep the UI thread responsive.

I don't know if you know how to work with threads, but there are several ways to have a code running in a different thread (Creating a new Thread, working with ThreadPool, working with BackgroundWorker, invoking a delegate, using .Net 4 Parallel etc).

One of the important things when working with threads is that you should never access the UI (or any UI elements) from a thread which is not the UI thread.
So if you want to change a progress bar value or update the text of a textbox from a different thread, then there are ways to do it.
(In WPF is using the Dispatcher of the UI Thread in order to invoke a delegate that will run on the UI thread)

If you need some code examples, please let me know.
0

Experts Exchange Solution brought to you by ConnectWise

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
 
JElsterAuthor Commented:
Thanks...
0
 
saraganiCommented:
Hi, here is an example of working on a different thread and also updating the UI:


<Window x:Class="TriggerWhileBusy.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <StackPanel>
            <ProgressBar x:Name="progressBar" Height="20" Visibility="Hidden"/>
            <Button Name="btnImport" Width="80" Grid.Row="2"
                                        Height="28"            
                                        Margin="25,5,10,5"
                                        HorizontalAlignment="Right" Content="Import" ToolTip="Import File " Click="btnImport_Click">


            </Button>
        </StackPanel>
    </Grid>
</Window>



Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Threading;

namespace TriggerWhileBusy
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void btnImport_Click(object sender, RoutedEventArgs e)
        {
            this.progressBar.Visibility = Visibility.Visible;
            this.progressBar.Value = 0;

            Thread thread = new Thread(DoSomethingThatTakesALongTimeToDo);
            thread.Start();

        }

        private void DoSomethingThatTakesALongTimeToDo()
        {
            Action action;
            for (int i = 0; i < 100; i++)
            {
                System.Threading.Thread.Sleep(150);

                action = delegate()
                {
                    this.progressBar.Value = i;
                };
                Application.Current.Dispatcher.Invoke(action);
            }

            action = delegate()
            {
                this.progressBar.Value = 100;
                this.progressBar.Visibility = Visibility.Hidden;
            };
            Application.Current.Dispatcher.Invoke(action);
        }
    }
}

Open in new window




Now, if instead of unzipping the file in your Click Event, you will unzip it in the DoSomethingThatTakesALongTimeToDo (Or any function that fun on a different thread) then you should not have any problem with a non-responding UI.
0
 
JElsterAuthor Commented:
Thanks!!!!!!!!!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.