1

Trying to retrieve a DialogResult from a window in a MVVM app, I stumbled on this previous question. After implementing the suggested changes, the sample looks like:

type DialogCloser() =

    static let DialogResultProperty =
        DependencyProperty.RegisterAttached("DialogResult", typeof<bool>, typeof<DialogCloser>, new PropertyMetadata(DialogResultChanged))

    static member GetDialogResult (a:DependencyObject) =
        a.GetValue(DialogResultProperty) :?> bool

    static member SetDialogResult (a:DependencyObject) (value:string) = 
        a.SetValue(DialogResultProperty, value)

    member this.DialogResultChanged (a:DependencyObject) (e:DependencyPropertyChangedEventArgs) =
        let window = a :?> Window
        match window with
        | null -> failwith "Not a Window"
        | _ -> window.DialogResult <- System.Nullable (e.NewValue :?> bool)

Now DialogResultChanged is used before it is declared, which of course doesn't compute in F#.

I can't seem to find a working solution, any help would be appreciated .

Community
  • 1
  • 1
Funk
  • 10,976
  • 1
  • 17
  • 33
  • 1
    I'm not a WPF expert, but in the C# solution (http://stackoverflow.com/questions/501886/how-should-the-viewmodel-close-the-form/3329467#3329467), the method `DialogResultsChanged` is static - if you define it as static, you should be able to reference it before it is declared (using `DialogCloser.DialogResultChanged`) - though you might need some type annotation. – Tomas Petricek Oct 21 '15 at 11:25
  • Got it working with Tomas's suggestion, thank you both. – Funk Oct 21 '15 at 11:46
  • @TomasPetricek can you add it as an answer please? – Random Dev Oct 21 '15 at 11:48
  • @Carsten Added, thanks :) Funk - did it work with the code below, or did you have to do some more changes? (I have not actually tested my version...) – Tomas Petricek Oct 21 '15 at 13:00
  • @TomasPetricek Works perfectly, now able to close the dialog from viewmodel and return result. – Funk Oct 21 '15 at 13:32

1 Answers1

3

I'm not a WPF expert, but in this related C# solution, the DialogResultsChanged method is static. If you define the method as static in F# too, you should be able to reference it before it is declared (using the full name DialogCloser.DialogResultsChanged), so something like the following should do the trick:

type DialogCloser() =

    static let DialogResultProperty =
        DependencyProperty.RegisterAttached
            ( "DialogResult", typeof<bool>, typeof<DialogCloser>, 
              new PropertyMetadata(DialogCloser.DialogResultChanged))

    static member GetDialogResult (a:DependencyObject) =
        a.GetValue(DialogResultProperty) :?> bool

    static member SetDialogResult (a:DependencyObject) (value:string) = 
        a.SetValue(DialogResultProperty, value)

    static member DialogResultChanged 
            (a:DependencyObject) (e:DependencyPropertyChangedEventArgs) =
        let window = a :?> Window
        match window with
        | null -> failwith "Not a Window"
        | _ -> window.DialogResult <- System.Nullable (e.NewValue :?> bool)
Community
  • 1
  • 1
Tomas Petricek
  • 240,744
  • 19
  • 378
  • 553