[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2011-05-12
20
Medium Priority
?
952 Views
Last Modified: 2013-11-12
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>
0
Comment
Question by:JElster
  • 11
  • 9
20 Comments
 
LVL 11

Expert Comment

by:saragani
ID: 35747193
It works for me...
0
 
LVL 11

Expert Comment

by:saragani
ID: 35747216
You do have btnImport_Click...
Make sure that you don't do any: e.Handled = true;
0
 
LVL 1

Author Comment

by:JElster
ID: 35747473
The progress.value never seems to change....
??????????????
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
LVL 11

Expert Comment

by:saragani
ID: 35747682
Do you have e.Handled =true in your click event?

Can you post your entire code? (xaml + cs)
0
 
LVL 1

Author Comment

by:JElster
ID: 35747724
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
 
LVL 11

Expert Comment

by:saragani
ID: 35747760
If one of youf functions get the "e", it might be doing whT I mentioned...
0
 
LVL 1

Author Comment

by:JElster
ID: 35747813
The other methods.. totally block the update..
0
 
LVL 11

Expert Comment

by:saragani
ID: 35747890
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
 
LVL 1

Author Comment

by:JElster
ID: 35747969
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
 
LVL 11

Expert Comment

by:saragani
ID: 35748042
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
 
LVL 1

Author Comment

by:JElster
ID: 35748148
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
 
LVL 11

Expert Comment

by:saragani
ID: 35748221
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
 
LVL 1

Author Comment

by:JElster
ID: 35748258
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
 
LVL 11

Expert Comment

by:saragani
ID: 35748375
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
 
LVL 11

Expert Comment

by:saragani
ID: 35748416
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
 
LVL 1

Author Comment

by:JElster
ID: 35748461
Yes... If I comment out the line it works
0
 
LVL 11

Accepted Solution

by:
saragani earned 2000 total points
ID: 35748561
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
 
LVL 1

Author Comment

by:JElster
ID: 35748665
Thanks...
0
 
LVL 11

Expert Comment

by:saragani
ID: 35748803
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
 
LVL 1

Author Comment

by:JElster
ID: 35749501
Thanks!!!!!!!!!!
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

868 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question