Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# discriminated unions and WPF databinding

I have an F# WPF application with view model classes. These classes need to be public in order to be used in the XAML as data types.

The problem is that it appears that when using F# type recursion (and) then the subordinate types are not accessible - or they appear not to be. That is to say that given this example:

type Foo () =

    member this.Bar = new Bar()

and Bar () =

    member this.Foo = new Foo()

then Bar is not accessible in the XAML.

I have tried to prefix Foo with public and Bar also. I've also tried to create a synonym. Neither of those changes solved the problem.

Is this really how it works or am I "doing it wrong"? If it is the way it works, can I then force the subordinate types (Bar, in the example) to be public?

Thanks! :-)

Update 1: On closer inspection (dotPeek) it appears that access is the same. The F# compiler creates a static outer class Foo and a nested class Foo. It does the same with Bar. They are all marked as public. Now I'm perplexed as to why Bar is not accessible from within XAML.

Update 2: Solved. WPF doesn't like discriminated unions. One or more of the subordinate types use discriminated unions and that prevents it from being used for XAML databinding.

like image 479
Bent Rasmussen Avatar asked Dec 29 '12 16:12

Bent Rasmussen


1 Answers

WPF doesn't like discriminated unions. One or more of the subordinate types use discriminated unions and that prevents it from being used for XAML databinding. Not quite obvious but that was the problem.

like image 153
Bent Rasmussen Avatar answered Nov 09 '22 23:11

Bent Rasmussen