How to access a valueconverter from xaml that is injected into codebehind.

BoVisfeldtBoVisfeldt DKMember ✭✭
edited May 2016 in Xamarin.Forms

I have a simple page ErrorPage.xaml that has a few labels.

<StackLayout>
    <Label x:Name="timestampLabel" Text="{Binding Timestamp}" VerticalOptions="Center"/>
    <Label Text="{Binding Operation}" Font="Bold" VerticalOptions="Center" />
<StackLayout/>

One the timeStampLabel should make use of the ReallyOddTimeFormatConverter.cs valueconverter that is injected into the codebehind ErrorPage.xaml.cs as interface IReallyOddTimeFormatConverter and set as a class-scope variable.

public ErrorPage(IReallyOddTimeFormatConverter dateTimeStringValueConverter,
        IErrorViewModel errorViewModel)
    {
       this.dateTimeStringValueConverter = dateTimeStringValueConverter;
       this.errorViewModel = errorViewModel;
       InitializeComponent();
       BindingContext = errorViewModel;
       ....
       ....
    }

Now, I could just make the binding from code-behind and set the valueconverter in the binding, but I want to do it using xaml if possible.

How can I alter my xaml to make use of the specific instance of the converter?

Sign In or Register to comment.