Reputation: 3980
I am using xamarin forms and using a collection within a collection my question is how do I bind to my lines when I use the ViewModel pattern.
I am using telerik data grid for the task, which can take any binding object as its Item Source. So my question is how do I access my sub list.
At the min I am doing ItemsSource="{Binding Boms}"
Can't I just do ItemsSource="{Binding Boms.Lines}"
when I try that I just get blank results.
<telerikGrid:RadDataGrid x:Name="gridItems" IsVisible="False" SelectionMode="Single"
SelectionChanged="GridItems_SelectionChanged" ItemsSource="{Binding Boms}"
AutoGenerateColumns="False">
<telerikGrid:RadDataGrid.Columns>
<telerikGrid:DataGridTextColumn PropertyName="Name" HeaderText="Name" />
<telerikGrid:DataGridTextColumn PropertyName="Qty" HeaderText="Qty" />
<telerikGrid:DataGridTemplateColumn x:Name="Actions" HeaderText="Scan Item">
<telerikGrid:DataGridTemplateColumn.CellContentTemplate>
<DataTemplate>
<telerikInput:RadButton
Grid.Row="0" Grid.Column="1" HeightRequest="40" Text="Scan Item" x:Name="btnScanItem"
Margin="0, 2, 0, 0" VerticalOptions="StartAndExpand" HorizontalOptions="Center"
BackgroundColor="Black" TextColor="White" />
</DataTemplate>
</telerikGrid:DataGridTemplateColumn.CellContentTemplate>
</telerikGrid:DataGridTemplateColumn>
<telerikGrid:DataGridTemplateColumn x:Name="Edit" HeaderText="Edit Item">
<telerikGrid:DataGridTemplateColumn.CellContentTemplate>
<DataTemplate>
<telerikInput:RadButton
Grid.Row="0" Grid.Column="1" HeightRequest="40" Text="Edit Item" Clicked="BtnScanItem_Clicked"
x:Name="btnScanItem" Margin="0, 2, 0, 0" VerticalOptions="StartAndExpand"
HorizontalOptions="Center"
BackgroundColor="Black" TextColor="White" />
</DataTemplate>
</telerikGrid:DataGridTemplateColumn.CellContentTemplate>
</telerikGrid:DataGridTemplateColumn>
</telerikGrid:RadDataGrid.Columns>
</telerikGrid:RadDataGrid>
My Code Behind
public class StockScanDataStore : IScanDataStore<Bom>
{
List<Bom> bomItems;
public StockScanDataStore()
{
bomItems = new List<Bom>();
var mockItems = new List<Bom>
{
new Bom { BomId =1,Name="Knee operations",Quantity=20,Operations=10 },
new Bom { BomId =1,Name="Back operations",Quantity=20,Operations=10 }
,
new Bom { BomId =1,Name="Steal Plate",Quantity=20,Operations=10,
Lines = new List<BomDetail>() {
new BomDetail{ BomId=1,Name="Screw",Qty=20} ,
new BomDetail{ BomId=2,Name="Plate",Qty=10 },
new BomDetail{ BomId=3,Name="Screw",Qty=5 }
}
} };
foreach (var item in mockItems)
{
bomItems.Add(item);
}
}
My Code Behind I set my view model as such
ScanTransViewModel viewModel;
public StockTransfer()
{
InitializeComponent();
BindingContext = viewModel = new ScanTransViewModel();
}
My model is:
public class Bom
{
public long BomId { get; set; }
public long BOMLineID { get; set; }
public long StockItemID { get; set; }
public string BomLineType { get; set; }
public decimal? Quantity { get; set; }
public long UnitID { get; set; }
public decimal? MultipleOfBaseUnit { get; set; }
public string Code { get; set; }
public string Name { get; set; }
public string Barcode { get; set; }
public long ProductGroupID { get; set; }
public string ProductGroupCode { get; set; }
public decimal Operations { get; set; }
public List<BomDetail> Lines { get; set; }
public override string ToString()
{
return Code;
}
}
Upvotes: 1
Views: 303
Reputation: 378
It looks like, in your code-behind, that you set the BindingContext AFTER InitializeContext(). Since I see no code that forces the view to update, I assume the problem is that the view inits as blank and then the list updates without the view following suit.
In your situation, I would try adding INotifyPropertyChanged interface to your code-behind. Then update your List like so -
protected virtual void NotifyPropertyChanged([CallerMemberName]string propertyName = null)
{
if (propertyName != null)
OnPropertyChanged(propertyName);
}
private List<Bom> _bomItems= new List<Bom>();
public List<IBlePeripheral> BomItems
{
get { return _bomItems; }
set
{
_bomItems= value;
NotifyPropertyChanged();
IsEmptyList = value.Count < 1;
}
}
Upvotes: 0