WPF IsEnabled 绑定

WPF IsEnabled binding

我无法理解如何在值不同于 550 时禁用文本框。如果初始值不同于 550,则禁用所有值,或者如果初始值等于 550,则为所有值启用。问题是当我更改 UI.

中的值时它不会更新

这是我的 xaml

<src:CustomTextBox VerticalAlignment = "Center"
                   Text="{Binding TrafoProperties.InsulationLevels.LightningImpulseVoltage,
                                  UpdateSourceTrigger = PropertyChanged,
                                  Mode = TwoWay,                    
                                  ValidatesOnNotifyDataErrors = True,
                                  NotifyOnValidationError = True}"
                    Validation.ErrorTemplate = "{StaticResource defaultErrorTemplate}"
                   IsEnabled="{Binding Path = TrafoProperties.InsulationLevels.IsEnabled, Mode = TwoWay}"/>

还有我的两个属性

public double LightningImpulseVoltage
{
    get { return _LightningImpulseVoltage; }
    set 
    { 
        SetProperty(ref _LightningImpulseVoltage, value);
        if (OnLightningImpulseVoltage != null)
            OnLightningImpulseVoltage();
    }
}

public bool IsEnabled 
{
    get { return LightningImpulseVoltage == 550; }
    set
    {
       OnPropertyChanged("LightningImpulseVoltage");
    }
}  

我的设置属性

protected bool SetProperty<T>(ref T storage, T value, [CallerMemberName] String propertyName = null)
{
     if (object.Equals(storage, value)) 
        return false;

     storage = value;
     this.OnPropertyChanged(propertyName);
     return true;
    }

还有我的 OnPropertyChanged

protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
    var eventHandler = this.PropertyChanged;
    if (eventHandler != null)
    {
        eventHandler(this, new PropertyChangedEventArgs(propertyName));
    }
}

您绑定到 IsEnabled,它依赖于 LightingImpulseVoltage。如果您希望在 LightingImpulseVoltage 值更改时更新该绑定,您需要在 that 属性 setter 中提高 PropertyChanged,例如这个:

public double LightningImpulseVoltage
{
    get { return _LightningImpulseVoltage; }
    set 
    { 
        SetProperty(ref _LightningImpulseVoltage, value);
        if (OnLightningImpulseVoltage != null)
            OnLightningImpulseVoltage();
        OnPropertyChanged("IsEnabled");
    }
}