XAML C#系结问题

wpx232ag  于 2022-12-07  发布在  C#
关注(0)|答案(1)|浏览(149)

我有以下代码。我的总计显示在wpf上,但是当我试图计算小计时,它不起作用。即使我的屏幕显示总计字段的值,它也只返回0。我已经厌倦了直接在屏幕上引用Total1字段,但是这也不起作用。需要得到一个小计,然后计算税收和总计。有什么想法吗?我只是看不到我遗漏了什么。

c#
private string total1;
private string total2;
private string total3;
private string total4;

public string Total1
    {
        get
        {
            //string ProdId = ((ComboBoxItem)Product1.SelectedItem).Tag;
            double qty1;
            bool good = double.TryParse(quantity1, out qty1);
            if (!(good)) qty1 = 0.0;
            double len1;
            bool good2 = double.TryParse(length1, out len1);
            if (!(good2)) len1 = 0.0;
            double pri1;
            bool good3 = double.TryParse(price1, out pri1);
            if (!(good3)) pri1 = 0.0;
            double res = qty1 * len1 * pri1;
            total1 = res.ToString();
            return total1;  
        }
        set
        {
            double qty1;
            bool good = double.TryParse(quantity1, out qty1);
            if (!(good)) qty1 = 0.0;
            double len1;
            bool good2 = double.TryParse(length1, out len1);
            if (!(good2)) len1 = 0.0;
            double pri1;
            bool good3 = double.TryParse(price1, out pri1);
            if (!(good3)) pri1 = 0.0;

            double res = qty1 * len1 * pri1;               
            total1 = res.ToString();
            OnPropertyChanged("Total1");
            OnPropertyChanged("SubTotal");
        }
    }
 public string SubTotal
    {
        get
        {
            double t1;
            bool good = double.TryParse(total1, out t1);
            if (!(good)) t1 = 0.0;
            double t2;
            bool good2 = double.TryParse(total2, out t2);
            if (!(good2)) t2 = 0.0;
            double t3;
            bool good3 = double.TryParse(total3, out t3);
            if (!(good3)) t3 = 0.0;
            double t4;
            bool good4 = double.TryParse(total4, out t4);
            if (!(good4)) t4 = 0.0;
            double res = t1 + t2 + t3 + t4; 
            return res.ToString();
        }
        set
        {
            double t1;
            bool good = double.TryParse(total1, out t1);
            if (!(good)) t1 = 0.0;
            double t2;
            bool good2 = double.TryParse(total2, out t2);
            if (!(good2)) t2 = 0.0;
            double t3;
            bool good3 = double.TryParse(total3, out t3);
            if (!(good3)) t3 = 0.0;
            double t4;
            bool good4 = double.TryParse(total4, out t4);
            if (!(good4)) t4 = 0.0;
            double res = t1 + t2 + t3 + t4;
            subTotal = res.ToString();
            OnPropertyChanged("SubTotal");
            OnPropertyChanged("TaxTotal");
            OnPropertyChanged("GrandTotal");
        }
    }

克马尔

<TextBox Grid.Column="11" Grid.Row="2" Width="80" Name="Total1" Height="24" 
    TextAlignment="Right" FontFamily="Arial" HorizontalAlignment="Center" IsReadOnly="True" 
    Text="{Binding Path=Total1, Mode=TwoWay}" />
<TextBox Grid.Column="7" Grid.Row="16" Width="80" Name="SubTotal" FontFamily="Arial" 
    TextAlignment="Right"  Height="24" HorizontalAlignment="Center" IsReadOnly="True"
    Text="{Binding Path=SubTotal, Mode=TwoWay}" />
oug3syen

oug3syen1#

有些事

  • 绑定支持转换为数字。
  • 由于四舍五入问题,在处理货币时使用小数点而不是双精度小数点

这应该行得通

public abstract class ObservableObject : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
    protected virtual void SetValue<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
    {
        field = value;
        OnPropertyChanged(propertyName);
    }
}

class ViewModel : ObservableObject
{

    private int _quantity;
    public int Quantity { get { return _quantity; } set { SetValue(ref _quantity, value); OnPropertyChanged(nameof(Total)); } }

    private decimal _length;
    public decimal Length { get { return _length; } set { SetValue(ref _length, value); OnPropertyChanged(nameof(Total)); } }

    private decimal _price;
    public decimal Price { get { return _price; } set { SetValue(ref _price, value); OnPropertyChanged(nameof(Total)); } }

    public decimal Total => Quantity * Length * Price;
}

克马尔

<Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="80"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <TextBlock Grid.Column="0" Grid.Row="0" Text="Quantity"/>
        <TextBox Grid.Column="1" Grid.Row="0" TextAlignment="Right"
            Text="{Binding Quantity, UpdateSourceTrigger=PropertyChanged}" />

        <TextBlock Grid.Column="0" Grid.Row="1" Text="Length"/>
        <TextBox Grid.Column="1" Grid.Row="1" TextAlignment="Right"
            Text="{Binding Length, UpdateSourceTrigger=PropertyChanged, StringFormat=#.##}" />

        <TextBlock Grid.Column="0" Grid.Row="2" Text="Price"/>
        <TextBox Grid.Column="1" Grid.Row="2" TextAlignment="Right"
            Text="{Binding Price, UpdateSourceTrigger=PropertyChanged, StringFormat=#.##}" />

        <TextBlock Grid.Column="0" Grid.Row="3" Text="Total"/>
        <TextBox Grid.Column="1" Grid.Row="3" TextAlignment="Right" IsReadOnly="True"
            Text="{Binding Total, Mode=OneWay, StringFormat=#.##}"/>
    </Grid>

相关问题