【问题标题】:Accessing a collection within a collection List Xamarin xaml访问集合 List Xamarin xaml 中的集合
【发布时间】: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


【解决方案1】:

看起来,在您的代码隐藏中,您在 InitializeContext() 之后设置了 BindingContext。由于我没有看到强制视图更新的代码,因此我认为问题在于视图初始化为空白,然后列表更新而视图不跟随。

在您的情况下,我会尝试将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;
            }
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-05
    • 1970-01-01
    • 2017-12-07
    • 2022-07-31
    • 1970-01-01
    • 2020-08-31
    相关资源
    最近更新 更多