gliderkite
gliderkite

Reputation: 8928

TabControl with ListView

I have a TabControl where each TabItem shows a ListView. The list is the same for each tab, but it is sorted by different fields depending on the tab selected.

So, I don't want that each TabItem contains a different ListView:

<TabControl>

    <TabItem>
        <ListView/>
    </TabItem>

    <TabItem>
        <ListView/>
    </TabItem>

</TabControl>

How can I accomplish this?

Upvotes: 1

Views: 3120

Answers (1)

Erre Efe
Erre Efe

Reputation: 15557

Quick and Easy. Let the code just the way it is now. Then have just one list on your ViewModel and you can have N diferent readonly properties (where N is the number of tabs) who will return the same list but sorted by your desired order.

The thing is:

I don't want that each TabItem contains a different ListView

But that's precisely what you're doing. Having a sorted-different list within each TabItem.

Example

View:

<TabControl>
    <TabItem>
        <ListView ItemsSource="{Binding SortedByX}" />
    </TabItem>
    <TabItem>
        <ListView ItemsSource="{Binding SortedByY}" />
    </TabItem>
    <TabItem>
        <ListView ItemsSource="{Binding SortedByZ}" />
    </TabItem>
</TabControl>

ViewModel:

public List<The type of your list items> List { get; set; }

public List<The type of your list items> SortedByX 
{
    get
    {
        return List.Sort(iComparer);
    }
}

// Or using LINQ

public List<The type of your list items> SortedByY 
{
    get
    {
        return List.OrderBy....
    }
}

Upvotes: 2

Related Questions