MultiBinding with different sources

John Hair 371 Reputation points
2021-08-11T14:00:14.993+00:00

Hi there,
Does MultiBinding support 2 different sources? E.g for example:

frame.SetBinding(Frame.BackgroundColorProperty, new MultiBinding
{
    Bindings = new Collection<BindingBase>
    {
        new Binding(nameof(_model1.Value1), source: _model1),
        new Binding(nameof(_model2.Value2), source: _model2),
    },
    Converter = new TestMultiConverter(),
});

The above appears to work in some circumstances, but not all and Im trying to track down why. Notify property changes will trigger the converter on 1 property, but not when notifying on the other property. But the other does work on a single binding.

Thanks
John

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,299 questions
{count} votes

1 answer

Sort by: Most helpful
  1. JarvanZhang 23,951 Reputation points
    2021-08-12T06:52:30.053+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    I created a basic demo to test the function and faced the same problem. If I set binding from the same model class in MultiBinding as below, it would work as expected. When changing the value of the property, the IMultiValueConverter will be triggered to update the data.

       <Label>  
           <Label.Text>  
               <MultiBinding Converter="{StaticResource _converter}">  
                   <Binding Path="TestModel.Title" />  
                   <Binding Path="TestModel.Content"/>  
               </MultiBinding>  
           </Label.Text>  
       </Label>  
    

    If using two different models in MultiBinding, there is nothing wrong with displaying the data. But IMultiValueConverter will not reevaluates when the data is changed.

       <Label >  
           <Label.Text>  
               <MultiBinding Converter="{StaticResource converter}">  
                   <Binding Path="TestModel.Title" />  
                   <Binding Path="TestModel2.Title"/>  
               </MultiBinding>  
           </Label.Text>  
       </Label>  
    

    In MultiBinding class, the Bindings property is of type IList<BindingBase>. For the List<T>, the type of list children should be the same. So Bindings may not support to use different binding type, but the document doens't specify this directly. You could report this problem to the Xamarin.Forms github repo to get the result.

    Best Regards,

    Jarvan Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.