WPF IsEnabled绑定

时间:2015-11-05 14:32:23

标签: c# .net wpf visual-studio .net-4.5

如果值不同于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");
    }
}  

我的SetProperty

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));
    }
}

1 个答案:

答案 0 :(得分:0)

您绑定的IsEnabled依赖于LightingImpulseVoltage。如果您希望在LightingImpulseVoltage值更改时更新该绑定,则需要在 属性设置器中引发PropertyChanged,如下所示:

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