Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declarative progress bar binding

Tags:

wpf

xaml

f#

After binding the command of a button to an action, I call an object which exposes a progress event.

event System.EventHandler<ProgressChangedEventArgs> ProgressChanged

I would like to display that in my XAML in the best way.


One way can be to expose two bindable fields in my VM

member x.Iteration with get()     = _iteration
                   and set(v:int) = _iteration <- v
                                    x.NotifyPropertyChanged <@this.Iteration@>

member x.IterationVisible with get()      = _iterationVisible
                          and set(v:bool) = _iterationVisible <- v
                                            x.NotifyPropertyChanged <@this.IterationVisible@>

then where I am called to perform the action I would just update the properties

member x.CompleteInference(algorithm:IGeneratedAlgorithm) =
    x.IterationVisible <- true
    algorithm.ProgressChanged.Add(fun args -> x.Iteration <- args.Iteration)
    algorithm.run()
    x.IterationVisible <- false

That leads to 2 questions :

  • Is there a direct way in F# to expose the progressChanged event, without going through this intermediate Iteration property, that can be processed by WPF ? Is it the most declarative we can get / do we always have to store some state somewhere ?

  • Additionaly, is there a natural way to do this 'state machine' binding entirely in XAML ?

like image 356
nicolas Avatar asked Mar 25 '13 12:03

nicolas


1 Answers

To my knowledge there is no way to handle events in xaml, so exposing the event changes through a property is probably the best you can do.

To achieve "'state machine' binding" in xaml, you could expose the progress as a size and then bind the width of your progress bar to that property. See here for an example of this.

like image 70
N_A Avatar answered Sep 21 '22 07:09

N_A