2017-12-13 6 views
1

私はUWPアプリケーションを持っています。xを使用するにはUWPを使用する最良の方法:ObservableCollectionをバインドしてフィルタリングします

ピボットコントロール付きの1つのビュー。

関連付けられた1つのViewModel。このような

1つのモデル:私は、各モデルでピボットコントロールでモデルを表示したい

public ObservableCollection<Model> models = new ObservableCollection<Model>(); 

:ViewModelにで

class Model 
{ 
    public int Category { get; set; } 
} 

、のようなモデル「モデル」ののObservableCollectionがありますピボットアイテム内のカテゴリによって。たとえば、Pivo​​tItem 1はカテゴリ1のすべてのモデルをカウントダウンし、Pivo​​tItem 2はカテゴリ2のモデルをすべてカウントダウンします。

解決策はカテゴリ別にフィルタリングされたモデルごとに新しいObservableCollectionを作成することです。私の意見では少し重い。たとえば、次のようにします。

public ObservableCollection<Model> modelCategory1 = models.Where(x => x.category == 1) [...] 

XAMLから直接フィルタリングするソリューションがないのではないかと私は思っていました。

EDIT:彼の見解で

、ピボットで、私は1つのListView

<Pivot> 
    <PivotItem> 
     <ListView ItemsSource="{x:Bind ViewModel.Models}" /> 
    </PivotItem> 
    <PivotItem> 
     <ListView ItemsSource="{x:Bind ViewModel.Models}" /> 
    </PivotItem> 
    <PivotItem> 
     <ListView ItemsSource="{x:Bind ViewModel.Models}" /> 
    </PivotItem> 
    <PivotItem> 
     <ListView ItemsSource="{x:Bind ViewModel.Models}" /> 
    </PivotItem> 
    <PivotItem> 
     <ListView ItemsSource="{x:Bind ViewModel.Models}" /> 
    </PivotItem> 
</Pivot> 

EDIT 2を含む5 pivotitemsそれぞれ持っている:i「ができ[UWP]CollectionViewSource Filter?:このCan I filter a collection from xaml?とこれによると

をUWPのCollectionViewSourceのtフィルタを使用しているため、新しいObservableCollectionを作成する必要があります.Filterの結果は次のようになります。

private ObservableCollection<Model> _modelsCategory1; 
public ObservableCollection<Model> ModelsCategory1 
{ 
    get { return _modelsCategory1; } 
    set { _modelsCategory1= value; NotifyPropertyChanged(); 
} 

と:

var cat1 = from fobjs in Models 
     where fobjs.Category == 1 
     select fobjs; 
ModelsCategory1 = new ObservableCollection<Model>(cat1); 
+0

あなたはデータバインディングを使用している場合、あなたはあなたの中に別のプロパティが必要になりますバインドするピボット・ヘッダーのモデル・クラス。 –

+0

UI部分を表示するように編集しました – ArthurCPPCLI

答えて

0

のListViewItemにカテゴリープロパティをバインドするために、既存のコードでこれを行い、

<PivotItem> 
    <ListView ItemsSource="{x:Bind ViewModel.Models}"> 
     <ListView.ItemTemplate> 
      <DataTemplate x:DataType="data:Model"> 
       <TextBlock Text="{x:Bind Category}"/> 
      </DataTemplate> 
     </ListView.ItemTemplate> 
    </ListView> 
</PivotItem> 
+0

これは私のリストにすべてのカテゴリを表示しますが、私の問題は各PivotItemのカテゴリに応じてViewModel.Modelsにメンバーを表示することです。最初のPivotItemは "ViewModel.Models category == 1 "の場合、2番目のPivotItemは" ViewModel.Models where category == 2 "などと一致するモデルのみを表示します – ArthurCPPCLI

関連する問題