2017-02-08 42 views
-1

在Xamarin窗體中,我有一個列表視圖,顯示項目表的所有項目。我只想顯示已批准'狀態'的項目。我怎樣才能做到這一點?我在哪裏通過'where status ='approved''條件。想要獲得的數據有條不紊地顯示在列表視圖

這是我的XAML代碼:

<ViewModels:ItemViewModel/> 

</ContentPage.BindingContext> 

<ListView ItemsSource="{Binding ItemsList}" 

      HasUnevenRows="True" 
      x:Name="lstItems" 
     ItemTapped="LstItems_OnItemTapped"> 





    <ListView.ItemTemplate> 

     <DataTemplate> 

      <ViewCell> 

       <StackLayout Orientation="Horizontal"> 

        <Label Text="{Binding ItemCode}" TextColor="Blue"/> 
       <Label Text="{Binding ItemDesc}" TextColor="Blue"/> 


       </StackLayout> 

      </ViewCell> 

     </DataTemplate> 


    </ListView.ItemTemplate> 

</ListView> 

我的視圖模型如下:

命名空間MyFirstDbApp.ViewModels { 公共類ItemViewModel:INotifyPropertyChanged的 { private List _itemsList;

public List<Item> ItemsList 



    { 
     get { return _itemsList; } 

     set 
     { 
      _itemsList = value; 
      OnPropertyChanged(); 
     } 
    } 

    public ItemViewModel() 
    { 

     InitializeDataAsync(); 
    } 


    private async Task InitializeDataAsync() 
    { 

     var ItemServices = new ItemServices(); 

     ItemsList = await ItemServices.GetItemsAsync(); 

    } 


    public event PropertyChangedEventHandler PropertyChanged; 

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) 
    { 
     PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 
    } 
} 

}上執行我的新代碼

錯誤:

變種結果=等待ItemServices.GetItemsAsync()ConfigureAwait(); ItemsList = result.Where(X => x.Status == 「已批准」

線:是所必需的非靜態字段,方法或屬性1個-An對象引用ItemServices.GetItemsAsync

線:1,無重載方法 'ConfigureAwait' 取0 argurments

行:2 - 不能轉換SourceType中 'Systems.Collection.Generic.IEnumerable' 爲目標類型 '系統 Colelction.GenericList'

+0

只是過濾在您的標準清單的基礎。你有什麼嘗試?告訴我們你的代碼。 – Cheesebaron

+0

我已經在問題本身中添加了我的xaml代碼。這將我的sql中的所有項返回到listview。我只想在條件列表視圖中僅顯示一些項目。我如何以及在哪裏通過條件? – user1508599

回答

0

篩選應在ViewModel中的ItemsList上完成,而不是在i上完成n XAML。

_viewModel.ItemsList.Where(x => x.Status == "Approved"); 

好運:-)

+0

嘿!我對xamarin發展非常陌生。我在問題中也添加了我的ViewModel代碼。你是否也可以告訴我在視圖模型中添加代碼的位置?非常感謝你... – user1508599

+0

in InitializeDataAsync .. var result = await ItemServices.GetItemsAsync()。ConfigureAwait(); ItemsList = result.Where(x => x.Status ==「Approved」); – Mittchel

+0

我有錯誤顯示代碼。在我的問題中提到。可以解釋如何解決這個問題。會有很大的幫助。 PLS。 – user1508599

相關問題