WPF-控件的属性值的类型转化

控件的属性值需要转成int、double进行运算的,可以使用一下方法

页面代码

  <StackPanel Margin="4,0,0,0" Style="{StaticResource Form-StackPanel}">
                                <Label Content="替换后材料增加金额:" Style="{StaticResource StackPanel-Label-Multiple}"/>
                                <c1:C1MaskedTextBox Style="{StaticResource StackPanel-C1MaskedTextBox-Multiple}" Text="{Binding CurrentParamReviewItem.MaterialIncreaseCost, Mode=TwoWay}" Name="MaterialIncreaseCost" Width="130" TextChanged="C1MaskedTextBox_TextChanged" />
                                <Label Content="替换后加工成本增加金额:" Style="{StaticResource StackPanel-Label-Multiple}"/>
                                <c1:C1MaskedTextBox Style="{StaticResource StackPanel-C1MaskedTextBox-Multiple}" Text="{Binding CurrentParamReviewItem.ProcessIncreaseCost, Mode=TwoWay}" Name="ProcessIncreaseCost" Width="130" TextChanged="C1MaskedTextBox_TextChanged" />
                            </StackPanel>
                          
                            <StackPanel Orientation="Horizontal">
                                <Label Content="替换后总成本变化:" Style="{StaticResource StackPanel-Label-Multiple}"/>
                                <c1:C1MaskedTextBox Style="{StaticResource StackPanel-C1MaskedTextBox-Multiple}" Text="{Binding CurrentParamReviewItem.TotalCost, Mode=TwoWay}" Name="TotalCost" Width="130" />
                              </StackPanel>

首先加入控件输入触发事件TextChanged,在对应的事件方法里面写入一下代码

 private void C1MaskedTextBox_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            double? materialIncreaseCost = 0;
            double? processIncreaseCost = 0;
            if (!string.IsNullOrEmpty(MaterialIncreaseCost.Text)) {
                materialIncreaseCost = Convert.ToDouble(MaterialIncreaseCost.Text);
            }
            if (!string.IsNullOrEmpty(ProcessIncreaseCost.Text))
            {
                processIncreaseCost = Convert.ToDouble(ProcessIncreaseCost.Text);
            }
            if (!materialIncreaseCost.HasValue) {
                materialIncreaseCost = 0;
            }
            if (!processIncreaseCost.HasValue)
            {
                processIncreaseCost = 0;
            }
            double sum = materialIncreaseCost.Value + processIncreaseCost.Value;
            double roundedSum = Math.Round(sum, 2, MidpointRounding.AwayFromZero);

            // 现在你可以使用roundedSum变量了,比如更新UI或进行其他计算
            // 例如,更新另一个控件的Text属性来显示结果
            TotalCost.Text = roundedSum.ToString("N2");

        }

效果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值