Home > Enterprise >  C# binding Questions
C# binding Questions

Time:08-04

I have the following code. My totals show on the wpf, but when I try to compute my subtotal, it does not work. Simply returns 0 even though my screen is showing a value for the total fields. I have tired referencing the Total1 field direct on the screen, but that did not work either. Need to get a subtotal, then compute taxes and a grandtotal. Any ideas? I just can't see what I am missing.

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

Xaml

<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}" />

CodePudding user response:

Some things

  • Bindings support conversion to numbers.
  • when dealing with money use decimal over double because of rounding issues

this should work

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

Xaml

    <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>
  • Related