Houman
Houman

Reputation: 66320

MultiBinding doesn't pick up the second Property

I have a problem with the MultiBinding. It seems the latest value of the second property is not picked up when the first property changes.

<Image Width="16" Source="../Images/YellowScales.png" Grid.Column="1" >
    <Image.Visibility>
         <MultiBinding Converter="{Converters:GoldScaleConverter}">
              <Binding Path="IsFavourite"/>                                            
              <Binding Path="MemoryUsageLevel"/>
         </MultiBinding>
    </Image.Visibility>
 </Image>

In the ViewModel:

public bool IsFavourite
        {
            get { return _isFavourite; }
            set
            {
                if (_isFavourite == value)
                    return;

                _isFavourite = value;

                RaisePropertyChanged("IsFavourite");
                UnloadBookCommmand.RaiseCanExecuteChanged();
            }
        }

public double MemoryUsageLevel
        {
            get
            {
                return GetMemoryUsageLevel(this);
            }
        }

Initially when I start the app, both properties are hit from the Converter and it works as expected.

However once the app is running and I change the IsFavourite property, it does trigger the multibinding and I can see withing the Converter that IsFavourite has flipped but the second value that is MemoryUsageLevel is always 0.0. The getter is not hit again.

But why I thought the MultiBinding is meant to check the latest value of both Bindings?

This is the converter:

public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            bool isFavourite = (bool) values[0];
            double memoryWarningLevel = (double) values[1];

            if(isFavourite && (memoryWarningLevel >= 50.00 && memoryWarningLevel < 75.00))
            {
                return Visibility.Visible;
            }
            return Visibility.Collapsed;

        }

Upvotes: 1

Views: 256

Answers (1)

Vinit Sankhe
Vinit Sankhe

Reputation: 19885

You probably have to raise PropertyChanged notification for MemoryUsageLevel as well. You can raise this in the setter of IsFavourite

   public bool IsFavourite 
   {
       get { .. }
       set {
         ...
         RaisePropertyChanged("IsFavourite"); 
         RaisePropertyChanged("MemoryUsageLevel"); 
       }  
   } 

Upvotes: 1

Related Questions