Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BindingError during binding to viewmodel's interface implementation via F#

Tags:

c#

mvvm

wpf

f#

Hello guys and sorry for such a long question, but...

I have a C# interface like this one:

public interface ISimpleViewModel
{
    string SimpleText { get; }
}

Then I have a F# type inherited from it:

type SimpleViewModel() = 
    interface ISimpleViewModel with
        member this.SimpleText
            with get() = "Hello again!"

Also I have a C# inheritor:

public class SimpleCSViewModel : ISimpleViewModel
{
    public string SimpleText
    {
        get { return "Just testing"; }
    }
}

Eventually I have a super simple WPF application which MainWindow ctor is injected with ISimpleViewModel instance this way:

public partial class MainWindow : Window
{
    public MainWindow(ISimpleViewModel viewModel)
    {
        ViewModel = viewModel;
        InitializeComponent();
    }

    public ISimpleViewModel ViewModel
    {
        get { return DataContext as ISimpleViewModel; }
        set { DataContext = value; }
    }
}

And of course I have a TextBlock on my Window which Text Property is binded to SimpleText.

Everyting works in case when I'm injecting C# instance. But I receive BindingError that property could be found in case of F# instance. Why it could be so?

like image 243
fxdxpz Avatar asked Jul 16 '26 16:07

fxdxpz


1 Answers

I don't know enough about how WPF binding works to be sure, but one thing to keep in mind is that interfaces in F# are explicitly implemented (meaning that the implementing methods are actually non-public), but by default they're implicit in C#. What happens if you use an explicit interface implementation in C# instead? If this is indeed the problem then the simplest fix would just be to create a public property on your F# type which duplicates the behavior of the interface implementation.

like image 104
kvb Avatar answered Jul 18 '26 05:07

kvb