繁体   English   中英

C# 绑定问题

[英]C# binding Questions

我有以下代码。 我的总计显示在 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");
        }
    }

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

有些事

  • 绑定支持转换为数字。
  • 由于舍入问题,在处理金钱时使用小数而不是双倍

这应该工作

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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM