Reputation: 9825
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 ?
Upvotes: 8
Views: 340
Reputation: 3451
You look to be doing this correctly although I am new to F#.
Wpf is typically used with an MVVM pattern, so wiring your button to a command property and your progress bar to an int property is correct. Your command then does something to cause the progress value to change and bindings take care of the rest.
Handling events in xaml requires code behind, which personally I don't mind if it serves only to enhance the UI. It certainly shouldn't be interacting with other objects. That means no event handlers.
This brings in event to command behaviours which can be used to bind control events to commands. See msdn.
Hope this helps in some way.
Upvotes: 1
Reputation: 19897
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.
Upvotes: 1