简体   繁体   English

MultiBinding不会选择第二个属性

[英]MultiBinding doesn't pick up the second Property

I have a problem with the MultiBinding. 我的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: 在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. 最初,当我启动应用程序时,两个属性都从Converter命中,并且可以按预期工作。

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. 但是,一旦应用程序运行并且更改了IsFavourite属性,它就会触发多重绑定,并且可以看到Converter有了IsFavourite翻转,但是第二个值MemoryUsageLevel始终为0.0。 The getter is not hit again. 吸气剂不再被击中。

But why I thought the MultiBinding is meant to check the latest value of both Bindings? 但是为什么我认为MultiBinding旨在检查两个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;

        }

You probably have to raise PropertyChanged notification for MemoryUsageLevel as well. 您可能还必须为MemoryUsageLevel引发PropertyChanged通知。 You can raise this in the setter of IsFavourite 您可以在IsFavourite的二传手中IsFavourite

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM