UI is not updating in xamarin forms

Prashant Sharma 66 Reputation points
2021-03-26T19:20:30.807+00:00

I am binding my model with my UI and in my model i have done some calculation but other properties are binding with UI but some properties in which i have done calculation these are not binding with my UI but showing the calculation in my OnPropertyChange event.Kindly help me on this where is issue on my codes Thanks in advance.

-----My model----

public class SaleEntryModel   
  {  
[PrimaryKey, AutoIncrement]  

public int SaleID { get; set; }  

  
public string CustomerName { get; set; }  

public int ProductID { get; set; }  

public string ProductName { get; set; }  

public decimal Quantity { get; set; }  
         
public decimal Rate { get; set; }  

public decimal Total => Rate * Quantity;  

public decimal Balance => (Total - (Discount + PaidAmount));  

}

-- I am calculating the total and balance from the rate and quantity properties---

----OnPropertyChange event ---

private SaleEntryModel bindSaleEntryModel = new SaleEntryModel();  

public SaleEntryModel BindSaleEntryModel  
{  
    get { return bindSaleEntryModel; }  
    set  
    {  
        bindSaleEntryModel = value;  
        OnPropertyChanged(nameof(BindSaleEntryModel));  
    }  
}  

I am binding my model with my UI and in my model i have done some calculation but other properties are binding with UI but some properties in which i have done calculation these are not binding with my UI but showing the calculation in my OnPropertyChange event.Kindly help me on this where is issue on my codes Thanks in advance.

-----My model----

 public class SaleEntryModel   
  {  
   [PrimaryKey, AutoIncrement]  

  public int SaleID { get; set; }  

  
public string CustomerName { get; set; }  

public int ProductID { get; set; }  

public string ProductName { get; set; }  

public decimal Quantity { get; set; }  
         
public decimal Rate { get; set; }  

public decimal Total => Rate * Quantity;  

public decimal Balance => (Total - (Discount + PaidAmount));  

}
-- I am calculating the total and balance from the rate and quantity properties---

----OnPropertyChange event ---

private SaleEntryModel bindSaleEntryModel = new SaleEntryModel();  

public SaleEntryModel BindSaleEntryModel  
{  
    get { return bindSaleEntryModel; }  
    set  
    {  
        bindSaleEntryModel = value;  
        OnPropertyChanged(nameof(BindSaleEntryModel));  
    }  
}  

---my xaml code ---

   <StackLayout Orientation="Vertical" HorizontalOptions="FillAndExpand"   
       VerticalOptions="FillAndExpand" Padding="10">  

     
        <Label  Text="Rate" Margin="2,-10" FontAttributes="Bold" />  


        <Entry x:Name="Rate" Margin="2,-5,2,5" Text="{Binding BindSaleEntryModel.Rate,Mode=TwoWay}"  
               HorizontalOptions="FillAndExpand" Keyboard="Numeric" ReturnType="Next" />  

        <Label x:Name="RateError" Margin="2,-10,2,5" TextColor="Red" IsVisible="false" FontAttributes="Italic" />  

        <Label Text="Quantity" Margin="2,-10" FontAttributes="Bold" />  

        <Entry x:Name="Quantity"  Margin="2,-5,2,5" Text="{Binding BindSaleEntryModel.Quantity,Mode=TwoWay}"  
               HorizontalOptions="FillAndExpand" Keyboard="Numeric" ReturnType="Next" />  
        <Label x:Name="QuantityError" Margin="2,-10,2,5" TextColor="Red" IsVisible="false" FontAttributes="Italic" />  

        <Label Text="Total" Margin="2,-10" FontAttributes="Bold" />  

        <Entry x:Name="Total" Margin="2,-5,2,5" IsEnabled="False"  
               Text="{Binding BindSaleEntryModel.Totals,Mode=TwoWay}"  
               HorizontalOptions="FillAndExpand" ReturnType="Next"/>  

        <Label Text="Discount (Rs)" Margin="2,-10" FontAttributes="Bold" />  

        <Entry x:Name="Discount" Margin="2,-5,2,5" Text="{Binding BindSaleEntryModel.Discount,Mode=TwoWay}"  
               HorizontalOptions="FillAndExpand"   
               Keyboard="Numeric" ReturnType="Next"/>  
        <Label x:Name="DiscountError" Margin="2,-10,2,5" TextColor="Red" IsVisible="false" FontAttributes="Italic" />  

        <Label Text="Paid Amount" Margin="2,-10" FontAttributes="Bold" />  

        <Entry x:Name="PaidAmount" Margin="2,-5,2,5" Text="{Binding BindSaleEntryModel.PaidAmount,Mode=TwoWay}"  
               HorizontalOptions="FillAndExpand" Keyboard="Numeric" ReturnType="Next"/>  
        <Label x:Name="PaidAmountError" Margin="2,-10,2,5" TextColor="Red" IsVisible="false" FontAttributes="Italic" />  

        <Label Text="Balance" Margin="2,-10" FontAttributes="Bold" />  

        <Entry x:Name="Balance"  Margin="2,-5,2,5" IsEnabled="False"  
               Text="{Binding BindSaleEntryModel.Balance,Mode=TwoWay}"  
               HorizontalOptions="FillAndExpand" />  

          
        <Grid HorizontalOptions="FillAndExpand">  
            <Grid.ColumnDefinitions>  
                <ColumnDefinition Width="1*" />  
                <ColumnDefinition Width="1*" />  
            </Grid.ColumnDefinitions>  

            <Button Text="Save" x:Name="btnSave" HorizontalOptions="FillAndExpand"   
                    CornerRadius="10" BorderWidth="2" BackgroundColor="#ff6633" TextColor="#fff" Margin="2"   
                    Grid.Column="0" Grid.Row="0" Command="{Binding SaveCommand}" />  

            <Button Text="CLEAR" x:Name="btnClear" HorizontalOptions="FillAndExpand"  
                    CornerRadius="10" BorderWidth="2" BackgroundColor="#bfbfbf"   
                    TextColor="#fff" Margin="2" Grid.Column="1" Grid.Row="0" Command="{Binding ClearCommand}" />  
        </Grid>  


    </StackLayout>  

81889-165795174-3827672310648539-5617411714763010557-n.jpg

In above image calculation is showing in propertychange event but not binding in My UI.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,289 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,193 questions
0 comments No comments
{count} votes

Accepted answer
  1. JessieZhang-MSFT 7,701 Reputation points Microsoft Vendor
    2021-03-29T03:15:45.39+00:00

    Hello,

    Welcome to our Microsoft Q&A platform!

    I found you have wrote the wrong property name when binding. For example : we should use BindSaleEntryModel.Total not BindSaleEntryModel.Totals .

    You can try to change the following code :

         <Entry x:Name="Total" Margin="2,-5,2,5" IsEnabled="False"  
                Text="{Binding BindSaleEntryModel.Totals,Mode=TwoWay}"  
                HorizontalOptions="FillAndExpand" ReturnType="Next"/>  
    

    to

             <Entry x:Name="Total" Margin="2,-5,2,5" IsEnabled="False"  
                Text="{Binding BindSaleEntryModel.Total,Mode=TwoWay}"  
                HorizontalOptions="FillAndExpand" ReturnType="Next"/>  
    

    Best Regards,

    Jessie Zhang

    ---
    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful