3

For Xamarin.Forms - XAML files:

is there a way to bind the Text Property (in XAML) of a Label to a Binding + DynamicResource? Maybe with string format?

for example I tried something like this:

<Label Text="{DynamicResource resource, Binding binding, StringFormat='Resource: {0} and Binding: {1}"} />

But one cannot declare a binding if a dynamic resource is set, same problem if trying it vice versa (eg. no dynamicresource if binding already set)

  • or with a value converter that returns the binding string to "binding string + dynamic resource"? (Creating a valueconverter for this seems too overwhelming)
  • in code this might work with string.Format(...)
Csharpest
  • 1,258
  • 14
  • 32

2 Answers2

2

Seems like MultiBinding is not supported in Xamarin.Forms applications.

This is a nice workaround that kind of implements full multibinding support for Xamarin:

http://intellitect.com/multibinding-in-xamarin-forms/

Here is a simpler implementation ready to be used:

https://gist.github.com/Keboo/0d6e42028ea9e4256715

And a discussion about the subject:

https://forums.xamarin.com/discussion/21034/multibinding-support

Isma
  • 14,604
  • 5
  • 37
  • 51
  • Thanks alot, http://intellitect.com/multibinding-in-xamarin-forms/ looks like a promising way to go. I will investigate on this and update this post, if I get a nice result. – Csharpest Sep 06 '17 at 13:26
  • 1
    I added a new link to my answer with a simple ready to use class ;) – Isma Sep 06 '17 at 13:42
  • Thanks alot, how will I handle this question now? Tick your answer as correct, or wait for me to update it, as soon as I upload my working solution, and tick my upload then? – Csharpest Sep 06 '17 at 14:18
  • Well, If you think I provided an answer to the question: Is there a way to bind the Text Property (in XAML) of a Label to a Binding + DynamicResource? then mark it as an answer but that's up to you of course. – Isma Sep 06 '17 at 14:29
  • You are right. And I can still edit my post to show my solution based on your answer! Have a nice day – Csharpest Sep 06 '17 at 14:39
0

I think what you need is MultiBinding.

Try creating a converter class like this:

public class MultiBindingConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return values[0].ToString() + " " + values[1].ToString();
    }
    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Reference it in your App.xaml or other resource dictionary

<local:MultiBindingConverter x:Key="MultiBindingConverter" />

Then do something like this in your View:

<Label>
    <Label.Content>
        <MultiBinding Converter="{StaticResource MultiBindingConverter}">
            <Binding Path="FirstProperty" />
            <Binding Path="SecondProperty" />
        </MultiBinding>
    </Label.Content>
</Label>

FirstProperty and SecondProperty are just regular properties in ViewModel.