\n
In above image calculation is showing but not binding in My UI.
\n","author":{"@type":"Person","name":"Prashant Sharma"},"upvoteCount":0,"answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"You need raise property change for each binding property in SaleEntryModel. Please refer following code.
\n public partial class CalculationQ : ContentPage\n {\n public CalculationQ()\n {\n InitializeComponent();\n this.BindingContext = this;\n GetSaleEntry();\n }\n\n private SaleEntryModel bindSaleEntryModel = new SaleEntryModel();\n public SaleEntryModel BindSaleEntryModel\n {\n get { return bindSaleEntryModel; }\n set\n {\n bindSaleEntryModel = value;\n OnPropertyChanged(nameof(BindSaleEntryModel));\n }\n }\n\n private void GetSaleEntry()\n {\n BindSaleEntryModel.SaleID = 1;\n BindSaleEntryModel.CustomerName = "Murugan";\n BindSaleEntryModel.ProductID = 1;\n BindSaleEntryModel.ProductName = "Toy";\n BindSaleEntryModel.Quantity = 5;\n BindSaleEntryModel.Rate = 150;\n BindSaleEntryModel.Discount = 5;\n BindSaleEntryModel.PaidAmount = 250;\n }\n\n }\n\n public class SaleEntryModel : INotifyPropertyChanged\n {\n\n public int SaleID { get; set; }\n\n private string _customerName;\n public string CustomerName\n {\n get { return _customerName; }\n set\n {\n _customerName = value;\n OnPropertyChange(nameof(CustomerName));\n }\n }\n\n public int ProductID { get; set; }\n\n private string _productName;\n public string ProductName\n {\n get { return _productName; }\n set\n {\n _productName = value;\n OnPropertyChange(nameof(ProductName));\n }\n }\n\n private decimal _quantity;\n public decimal Quantity\n {\n get { return _quantity; }\n set\n {\n _quantity = value;\n OnPropertyChange(nameof(Quantity));\n OnPropertyChange(nameof(Total));\n OnPropertyChange(nameof(Balance));\n }\n }\n\n private decimal _rate;\n\n\n public decimal Rate\n {\n get { return _rate; }\n set\n {\n _rate = value;\n OnPropertyChange(nameof(Rate));\n OnPropertyChange(nameof(Total));\n OnPropertyChange(nameof(Balance));\n }\n }\n\n public decimal Total => Rate * Quantity;\n\n public decimal Balance => (Total - (Discount + PaidAmount));\n\n private int _discount;\n public int Discount\n {\n get => _discount;\n set\n {\n _discount = value;\n OnPropertyChange(nameof(Discount));\n OnPropertyChange(nameof(Balance));\n }\n }\n\n private int _paidAmount;\n public int PaidAmount\n {\n get => _paidAmount;\n set\n {\n _paidAmount = value;\n OnPropertyChange(nameof(PaidAmount));\n OnPropertyChange(nameof(Balance));\n }\n }\n\n public event PropertyChangedEventHandler PropertyChanged;\n public void OnPropertyChange(string propName)\n {\n if (PropertyChanged != null)\n {\n PropertyChanged(this, new PropertyChangedEventArgs(propName));\n }\n }\n\n }\n
\nMy suggestion is to keep separate model for viewmodel binding instead of using entity model.
\n","author":{"@type":"Person","name":"Ranjit"},"upvoteCount":2}}}Reputation: 91
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>
In above image calculation is showing but not binding in My UI.
Upvotes: 0
Views: 784
Reputation: 863
You need raise property change for each binding property in SaleEntryModel. Please refer following code.
public partial class CalculationQ : ContentPage
{
public CalculationQ()
{
InitializeComponent();
this.BindingContext = this;
GetSaleEntry();
}
private SaleEntryModel bindSaleEntryModel = new SaleEntryModel();
public SaleEntryModel BindSaleEntryModel
{
get { return bindSaleEntryModel; }
set
{
bindSaleEntryModel = value;
OnPropertyChanged(nameof(BindSaleEntryModel));
}
}
private void GetSaleEntry()
{
BindSaleEntryModel.SaleID = 1;
BindSaleEntryModel.CustomerName = "Murugan";
BindSaleEntryModel.ProductID = 1;
BindSaleEntryModel.ProductName = "Toy";
BindSaleEntryModel.Quantity = 5;
BindSaleEntryModel.Rate = 150;
BindSaleEntryModel.Discount = 5;
BindSaleEntryModel.PaidAmount = 250;
}
}
public class SaleEntryModel : INotifyPropertyChanged
{
public int SaleID { get; set; }
private string _customerName;
public string CustomerName
{
get { return _customerName; }
set
{
_customerName = value;
OnPropertyChange(nameof(CustomerName));
}
}
public int ProductID { get; set; }
private string _productName;
public string ProductName
{
get { return _productName; }
set
{
_productName = value;
OnPropertyChange(nameof(ProductName));
}
}
private decimal _quantity;
public decimal Quantity
{
get { return _quantity; }
set
{
_quantity = value;
OnPropertyChange(nameof(Quantity));
OnPropertyChange(nameof(Total));
OnPropertyChange(nameof(Balance));
}
}
private decimal _rate;
public decimal Rate
{
get { return _rate; }
set
{
_rate = value;
OnPropertyChange(nameof(Rate));
OnPropertyChange(nameof(Total));
OnPropertyChange(nameof(Balance));
}
}
public decimal Total => Rate * Quantity;
public decimal Balance => (Total - (Discount + PaidAmount));
private int _discount;
public int Discount
{
get => _discount;
set
{
_discount = value;
OnPropertyChange(nameof(Discount));
OnPropertyChange(nameof(Balance));
}
}
private int _paidAmount;
public int PaidAmount
{
get => _paidAmount;
set
{
_paidAmount = value;
OnPropertyChange(nameof(PaidAmount));
OnPropertyChange(nameof(Balance));
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChange(string propName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}
}
My suggestion is to keep separate model for viewmodel binding instead of using entity model.
Upvotes: 2