代码之家  ›  专栏  ›  技术社区  ›  roozbeh S

如何仅在选中复选框时将TextBox绑定到可为空的属性

  •  0
  • roozbeh S  · 技术社区  · 6 年前

    我需要绑定 TextBox.Text 如果 CheckBox 在它旁边是选中的。否则,属性的值应为null。

    TextBox binding to nullable

    假设DTO类类似于:

    public class DataForInputDTO 
    {
        public double? Power {get; set;}
        public double? Speed {get; set;} 
        // Other Properties
    }
    

    现在,当用户取消选中 复选框 TextBox 它旁边被禁用(这是确定的),但我还需要该属性 速度 获取null而不是 60 CheckBox.CheckedChaneg

    1 回复  |  直到 6 年前
        1
  •  0
  •   Anu Viswan    6 年前

        private double _powerValue = 0;
    
        public double? Power { get; set; } = 0;
    
        public bool IsPowerEnabled
        {
            get => _isPowerEnabled;
            set
            {
                _isPowerEnabled = value;
                if (!value)
                {
                    _powerValue = Power.Value;
                    Power = null;
                }
                else
                {
                    Power = _powerValue;
                }
                NotifyOfPropertyChange(nameof(Power));
                NotifyOfPropertyChange(nameof(IsPowerEnabled));
            }
        }