WPF—TextBox 后台绑定double,如何让前台不显示默认0

          采用的方法是使用Nullable<double>代替double 。

           Nullable<double>和double的操纵对用户来说是透明的,可以无差别的混合使用它们。 但是Nullable<double>多了一个可以判断值类型是否有效的好处,这个体现到前台的好处就是TextBox可以显示空白。

   object d1 = 1.2;
   var b1 = d1 is Nullable<double>;    //true
   var b2 = d1 is double; //true

          但是这样写也有一个问题:当文本框的值被删除时,TextBox会显示红色边框。接下来怎么解决这个问题呢,加一个转换器就可以了。转换器代码

    public class StringEmptyToNull : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is string str && string.IsNullOrWhiteSpace(str))
            {
                return null;
            }
            return value;
        }
    }

加入转换器前: 

 加入转换器后:

 

 

所有测试代码:

前台:

<Window.Resources>
        <local:StringEmptyToNull x:Key="Converter"></local:StringEmptyToNull>
    </Window.Resources>
    <Grid>
        <StackPanel Margin="5" Orientation="Horizontal" VerticalAlignment="Center">
            <TextBox Text="{Binding A,UpdateSourceTrigger=PropertyChanged,Converter={StaticResource Converter}}" VerticalContentAlignment="Center" Width="60" Height="30" VerticalAlignment="Top"></TextBox>
            <TextBlock Text="+"  Height="30" VerticalAlignment="Top" Margin="5"></TextBlock>
            <TextBox Text="{Binding B,UpdateSourceTrigger=PropertyChanged,Converter={StaticResource Converter}}" VerticalContentAlignment="Center" Width="60" Height="30" VerticalAlignment="Top"></TextBox>
            <TextBlock Text="=" Margin="5"></TextBlock>
            <TextBox Text="{Binding C,UpdateSourceTrigger=PropertyChanged}"  VerticalContentAlignment="Center" Width="60" Height="30" VerticalAlignment="Top" IsReadOnly="True"></TextBox>
        </StackPanel>  
    </Grid>

后台:

 /// <summary>
    /// WinInteractive.xaml 的交互逻辑
    /// </summary>
    public partial class WinInteractive : Window
    {
        public WinInteractive()
        {
            InitializeComponent();
            this.DataContext = new InteractiveItem();
        }
    }


    public class InteractiveItem : INotifyPropertyChanged
    {
        #region 通知接口实现
        public event PropertyChangedEventHandler PropertyChanged;
        protected void RaisePropertyChanged([CallerMemberName]string propertyName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
        protected void SetPropertyValue<T>(ref T origin, T newValue, [CallerMemberName] string propertyName = "")
        {
            origin = newValue;
            RaisePropertyChanged(propertyName);
        }
        #endregion


        private Nullable<double> m_A;
        /// <summary>
        /// A参数
        /// </summary>
        public Nullable<double> A
        {
            get { return this.m_A; }
            set
            {
                this.m_A = value;
                C = m_A + m_B;
               RaisePropertyChanged(nameof(A));
            }
        }


        private Nullable<double> m_B;
        /// <summary>
        /// B参数
        /// </summary>
        public Nullable<double> B
        {
            get { return this.m_B; }
            set
            {
                this.m_B = value;
                C = m_A + m_B;
                RaisePropertyChanged(nameof(B));
            }
        }


        private Nullable<double> m_C;
        /// <summary>
        /// C参数
        /// </summary>
        public Nullable<double> C
        {
            get { return this.m_C; }
            set
            {
                this.m_C = value;
                RaisePropertyChanged(nameof(C));
            }
        }

       
    }

 

 

 

 

 

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值