DataGrid is not showing any value when it bind in XAML view code even window data context is defined in XAML all other textbox and combobox data binding on the same view is working fine all of these property are not pasted in following code
View Code
<Window x:Class="MegaSoft.Views.Windows.SaleInvoiceDetialWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MegaSoft.Views.Windows"
mc:Ignorable="d"
d:DesignHeight="1500" d:DesignWidth="1200"
xmlns:uc="clr-namespace:MegaSoft.UserControls"
xmlns:vm="clr-namespace:MegaSoft.ViewModel"
WindowState="Maximized"
Title="Sale Invoice">
<Window.DataContext>
<vm:SaleInvoiceDetialViewModel x:Name="_SaleInvoiceDetialViewModel"/>
</Window.DataContext>
<DataGrid MinHeight="300" MaxHeight="300" MaxWidth="1300" ItemsSource="{Binding Path=DataGridCollection,Mode=TwoWay,NotifyOnTargetUpdated=True,NotifyOnSourceUpdated=True,ValidatesOnDataErrors=True,UpdateSourceTrigger=PropertyChanged,ValidatesOnExceptions=True}" Name="SaleInvoiceDataGrid" RowDetailsVisibilityMode="VisibleWhenSelected" SelectionUnit="CellOrRowHeader" ColumnWidth="Auto" AutoGenerateColumns="False" CanUserAddRows="True" CanUserDeleteRows="True" CanUserReorderColumns="False" >
<DataGrid.Columns>
<DataGridTextColumn Header="Id" Visibility="Collapsed" Binding="{Binding Path=Id,Mode=OneWay ,ValidatesOnDataErrors=True,UpdateSourceTrigger=PropertyChanged,ValidatesOnExceptions=True}" Width="Auto" CanUserResize="False" ></DataGridTextColumn>
<DataGridTextColumn IsReadOnly="True" MaxWidth="100" Header="Sr. No" Binding="{Binding Path=SRNo,Mode=OneTime ,ValidatesOnDataErrors=True,UpdateSourceTrigger=PropertyChanged,ValidatesOnExceptions=True}" CanUserResize="False"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
</Window>
View is bind with view model which contain DataGrid Binding object some and other objects ViewModel
public class SaleInvoiceDetialViewModel : INotifyPropertyChanged, IDataErrorInfo
{
public ObservableCollection<SaleInvoiceDetialDataGridViewModel> DataGridCollection
{
get { return _DataGridCollection; }
set
{
_DataGridCollection = value;
OnPropertyChanged("DataGridCollection");
}
}
#region PropertyChange
//public event PropertyChangedEventHandler PropertyChanged;
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Implemantation of Property change interface
/// </summary>
/// <param name="property">Name of property</param>
protected void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
// other properties
}
Datagrid show that when i try that
_FrmSaleInvoiceDetialWindow.SaleInvoiceDataGrid.ItemsSource = DataGridCollection ;
But its not required. it should work as normal binding in Xmal
ItemsSource="{Binding Path=DataGridCollection}"
SaleInvoiceDetialDataGridViewModel
public class SaleInvoiceDetialDataGridViewModel : INotifyPropertyChanged {
GetGeneralData getData = new GetGeneralData();
public static int CountSRN { get; set; } = 1;
private int? _SRNO;
public int? SRNo
{
get
{
if (_SRNO.HasValue)
{
return ++_SRNO;
}
return _SRNO = CountSRN++;
}
set
{
_SRNO = value;
}
}
private int _Id;
public int Id
{
get { return _Id; }
set
{
_Id = value;
OnPropertyChanged("Id");
}
}
private int? _ItemAccountId;
public int? ItemAccountId
{
get { return _ItemAccountId; }
set
{
_ItemAccountId = value;
OnPropertyChanged("ItemAccountId");
OnPropertyChanged("ProductName");
OnPropertyChanged("IsCatchWeight");
OnPropertyChanged("CWSize");
OnPropertyChanged("CWQty");
OnPropertyChanged("Unit");
}
}
private string _ProductName;
public string ProductName
{
get
{
if (ItemAccountId.HasValue)
return _ProductName = ItemAccountList.Where(x => x.Id == _ItemAccountId).FirstOrDefault()?.ItemName;
return _ProductName;
}
set
{
_ProductName = value;
OnPropertyChanged("ProductName");
}
}
private bool? _IsCatchWeight;
public bool? IsCatchWeight
{
get
{
if (_ItemAccountId.HasValue)
return _IsCatchWeight = Convert.ToBoolean(_ItemAccountList.FirstOrDefault(x => x.Id == ItemAccountId)?.IsCatchWeightItem);
return _IsCatchWeight;
}
set
{
_IsCatchWeight = value;
OnPropertyChanged("IsCatchWeight");
}
}
private double? _CWSize;
public double? CWSize
{
get
{
//if (IsCatchWeight == true)
return _CWSize = _ItemAccountList?.FirstOrDefault(x => x.Id == ItemAccountId)?.CWSizeOrConversion;
//return _CWSize;
}
set
{
_CWSize = value;
OnPropertyChanged("CWSize");
OnPropertyChanged("Quantity");
OnPropertyChanged("ActualPriceAfterDiscount");
OnPropertyChanged("NetAmount");
}
}
private double? _CWQty;
public double? CWQty
{
get
{
if (IsCatchWeight != true)
return _CWQty = null;
return _CWQty;
}
set
{
_CWQty = value;
OnPropertyChanged("CWQty");
OnPropertyChanged("Quantity");
OnPropertyChanged("ActualPriceAfterDiscount");
OnPropertyChanged("NetAmount");
}
}
private double? _Quantity;
public double? Quantity
{
get
{
if (_IsCatchWeight == true)
{
if (MaxQuantity.HasValue && _MaxQuantity < (CWQty * CWSize))
{
CWQty = MaxCWQty;
}
return _Quantity = CWQty * CWSize;
}
if(MaxQuantity.HasValue && _Quantity > _MaxQuantity)
{
_Quantity = _MaxQuantity;
}
return _Quantity;
}
set
{
_Quantity = value;
OnPropertyChanged("Quantity");
OnPropertyChanged("MaxQuantity");
OnPropertyChanged("ActualPriceAfterDiscount");
OnPropertyChanged("NetAmount");
}
}
private string _Unit;
public string Unit
{
get
{
if (_ItemAccountId.HasValue)
return _Unit = _ItemAccountList?.FirstOrDefault(x => x.Id == ItemAccountId)?.UOM?.Name;
return _Unit;
}
set
{
_Unit = value;
OnPropertyChanged("Unit");
}
}
private float? _UnitPrice;
public float? UnitPrice
{
get { return _UnitPrice; }
set
{
_UnitPrice = value;
OnPropertyChanged("UnitPrice");
OnPropertyChanged("ActualPriceAfterDiscount");
OnPropertyChanged("NetAmount");
}
}
private float? _Discount;
public float? Discount
{
get { return _Discount; }
set
{
_Discount = value;
OnPropertyChanged("Discount");
OnPropertyChanged("ActualPriceAfterDiscount");
OnPropertyChanged("NetAmount");
}
}
private int? _DiscountPercent;
public int? DiscountPercent
{
get
{
if (_DiscountPercent == 0)
return _DiscountPercent = null;
return _DiscountPercent;
}
set
{
_DiscountPercent = value;
OnPropertyChanged("DiscountPercent");
OnPropertyChanged("ActualPriceAfterDiscount");
OnPropertyChanged("NetAmount");
}
}
private double? _ActualPriceAfterDiscount;
public double? ActualPriceAfterDiscount
{
get
{
return _ActualPriceAfterDiscount = (UnitPrice - (Discount ?? 0.0)) - ((DiscountPercent.HasValue == true) ? ((UnitPrice - (Discount ?? 0.0)) * (DiscountPercent / 100.0)) : 0.0);
}
set
{
_ActualPriceAfterDiscount = value;
OnPropertyChanged("ActualPriceAfterDiscount");
//OnPropertyChanged("NetAmount");
}
}
private double? _NetAmount;
public double? NetAmount
{
get
{
return _NetAmount = ActualPriceAfterDiscount * _Quantity;
}
set
{
_NetAmount = value;
OnPropertyChanged("NetAmount");
}
}
private int? _SiteId;
public int? SiteId
{
get { return _SiteId; }
set
{
_SiteId = value;
OnPropertyChanged("SiteId");
OnPropertyChanged("WarehouseList");
OnPropertyChanged("WarehouseId");
}
}
private int? _WarehouseId;
public int? WarehouseId
{
get
{
if (!_SiteId.HasValue)
return _WarehouseId = null;
return _WarehouseId;
}
set
{
_WarehouseId = value;
OnPropertyChanged("WarehouseId");
}
}
private double? _MaxQuantity;
public double? MaxQuantity
{
get { return _MaxQuantity; }
set
{
_MaxQuantity = value;
OnPropertyChanged("MaxQuantity");
}
}
private double? _MaxCWQty;
public double? MaxCWQty
{
get { return _MaxCWQty; }
set
{
_MaxCWQty = value;
OnPropertyChanged("MaxCWQty");
}
}
#region DropDownList
_ItemAccountList = new ObservableCollection<ItemAccount>();
private static ObservableCollection<ItemAccount> _ItemAccountList = GetGeneralData.GetItemAccountListStatic();
public static ObservableCollection<ItemAccount> ItemAccountList
{
get
{
if (_ItemAccountList.Any() == false)
return _ItemAccountList = GetGeneralData.GetItemAccountListStatic();
return _ItemAccountList;
}
set
{
_ItemAccountList = value;
//OnPropertyChanged("ItemAccountList");
}
}
private ObservableCollection<MiscList> _SiteList = new ObservableCollection<MiscList>();
public ObservableCollection<MiscList> SiteList
{
get
{
if (_SiteList.Any() == false)
return _SiteList = getData.GetSiteList();
return _SiteList;
}
set
{
_SiteList = value;
OnPropertyChanged("SiteList");
}
}
private ObservableCollection<MiscList> _WarehouseList;
public ObservableCollection<MiscList> WarehouseList
{
get
{
return _WarehouseList = getData.GetWarehouseList(SiteId);
}
set
{
_WarehouseList = value;
OnPropertyChanged("WarehouseList");
}
}
#endregion
#region PropertyChange
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
Mode=TwoWay
(and all the other Binding properties except Path) on the ItemsSource Binding is pointless. – Clemens