【发布时间】:2019-05-13 22:57:40
【问题描述】:
我正在使用 xamarin 表单并在集合中使用集合我的问题是当我使用 ViewModel 模式时如何绑定到我的行。
我正在为任务使用 Telerik 数据网格,它可以将任何绑定对象作为其项目源。所以我的问题是如何访问我的子列表。
至少我在做ItemsSource="{Binding Boms}"
当我尝试得到空白结果时,我不能只做ItemsSource="{Binding Boms.Lines}"。
<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>
我的代码背后
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);
}
}
我的代码在我这样设置我的视图模型
ScanTransViewModel viewModel;
public StockTransfer()
{
InitializeComponent();
BindingContext = viewModel = new ScanTransViewModel();
}
我的模型是:
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;
}
}
【问题讨论】:
-
如果 ScanTransViewModel 是您的 VM,那么查看该类的代码非常相关
-
如果
Boms.Lines有数据,当然可以绑定子列表如:ItemsSource="{Binding Boms.Lines}"
标签: xamarin xamarin.forms