1

I have a window which displays sales between a certain time in a restaurant/shop. When the user selects the time period to query, it shows sales data between that time. I am also Programatically creating a list of users which one can then select to filter a query. For example, I choose the 'Michael' user which is then used to show all sales that have been attributed to him (in the time frame previously selected).

Creating the ListView of users is fairly easy but I am trying to append this list with an item which would read 'All Users'. This would then be passed back to the query, which would then recognize this user by some Property (UserId = 999 or whatever. Not important) to populate the page with the data of all users again.

Right now I have to exit the page and go back in to do this. Not very elegant!

I was going to append a User object in the ViewModel to the list that is generated from the database EF but it creates a list of IUsers so I can't instantiate an actual instance of it (maybe I am being incredibly stupid here and am missing something fundamental?).

Any help in achieving this goal would be most appreciated.

akjoshi
  • 15,374
  • 13
  • 103
  • 121

2 Answers2

0

Your UI would typically create a view model that wraps the underlying user information. Then you would have a collection of these view models, to which the view binds. Assuming you have that, it is a simple matter of adding a sentinel instance to this collection. It might look something like this:

// this is your DAL class
public class User
{
}

// a view model to wrap the DAL class    
public class UserViewModel
{
    // a special instance of the view model to represent all users
    public static readonly UserViewModel AllUsers = new UserViewModel(null);
    private readonly User user;

    public UserViewModel(User user)
    {
        ...
    }

    // view binds to this to display user
    public string Name
    {
        get { return this.user == null ? "<<All>>" : this.user.Name; }
    }
}

public class MainViewModel()
{
    private readonly ICollection<UserViewModel> users;

    public MainViewModel()
    {
        this.users = ...;
        this.users.Add(UserViewModel.AllUsers);
    }

    public ICollection<UserViewModel> Users
    {
        ...
    }
}

In your code to construct the query, all you need to do is check whether the user in the user view model is present. If not, there is no need to append any user selection onto the query.

Kent Boogaart
  • 175,602
  • 35
  • 392
  • 393
0

You can try to use CompositeCollection to set ItemSource of your ListBox -

<ListBox> 
    <ListBox.ItemsSource> 
        <CompositeCollection> 
            <CollectionContainer Collection="{Binding YourCollection}" /> 
            <ListBoxItem Foreground="Magenta">Select All</ListBoxItem> 
        </CompositeCollection> 
    </ListBox.ItemsSource> 
</ListBox> 

But you will have to apply some workaround(like using BindingProxy) to make Binding work as CollectionContainer doesn't support bindings, refer these links -

http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/637b563f-8f2f-4af3-a7aa-5d96b719d5fd/

How do you bind a CollectionContainer to a collection in a view model?

Community
  • 1
  • 1
akjoshi
  • 15,374
  • 13
  • 103
  • 121