我正在使用xamrian形式,并且在一个集合中使用一个集合,我的问题是当我使用ViewModel模式时如何绑定到行。
我正在使用telerik数据网格执行任务,该网格可以将任何绑定对象用作其Item Source。所以我的问题是如何访问我的子列表。
我正在做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;
}
}
答案 0 :(得分:0)
在您的代码后面,您似乎设置了BindingContext AFTER InitializeContext()。由于我看不到任何强制视图更新的代码,因此我认为问题在于视图初始化为空白,然后列表更新而视图没有遵循。
在您的情况下,我会尝试向您的代码后面添加INotifyPropertyChanged接口。然后像这样更新您的列表-
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;
}
}