Search code examples
c#datetimedata-bindingxamarin.formscustom-renderer

Binding OneWayToSource in C#


I'm currently trying to receive the DateTime from a Xamarin.Forms.TimePicker. I just want to populate the model which is a DateTime pickerTime variable. This variable will be populated by whatever is in the TimePicker view.

Therefore I need to do OneWayToSource data binding but I only ever find examples in XAML, where as I'm working in C# file because its an Android custom render of a abstract Xamarin.Forms Page.

Is there any way to OneWayToSource data bind in C#? If not I would try making the Forms View page type of my abstract sub-classed page so I have access to a XAML page and make the TimePicker view as well the data bound object in there that the Custom Renderer Pages inherit with OneWayToSource data binding.

Thanks for your help!


Solution

  • Of course you can. Considering you have already set the BindingContext, this XAML:

    <TimePicker x:Name="myTimePicker" Time="{Binding Property, Mode=OneWayToSource}"/>
    

    is the same as this in C#:

    TimePicker myTimePicker = new TimePicker();
    myTimePicker.SetBinding(TimePicker.TimeProperty, new Binding("Property") { Mode = BindingMode.OneWayToSource });
    

    FYI, anything you can do in XAML can be done using C#!