Showing multiple models in a single ListView

Posted by Veer on Stack Overflow See other posts from Stack Overflow or by Veer
Published on 2010-06-17T09:20:56Z Indexed on 2010/06/17 9:23 UTC
Read the original article Hit count: 178

Filed under:
|
|
|

I've three models (Contacts, Notes, Reminders). I want to search all these and produce the filtered result in a single listview and depending upon the selection I've to display the corresponding view(UserControl) to its right.

I want the right way of implementing the design or atleast alternatives to this method that I've tried.

Now I've tried it using a IntegratedViewModel having all the properties from all the three models.

public class IntegratedViewModel
{
    ContactModel _contactModel;
    NoteModel _noteModel;

    public IntegratedViewModel(ContactModel contactModel)
    {
        _contactModel = contactModel;
    } // similarly for other models also

    public string DisplayTitle    // For displaying in ListView
    { 
        get; //same as set
        set 
        { 
            If(_contactModel != null) 
                return _contactModel.Name; 
            If(_noteModel != null)
                return _noteModel.Title;
        }
    }

    //  All other properties from the three models includin the Name/Title properties for displaying them in the corresponding views(UserControl)
}

Now I set the itemsSource as the List<IntegratedViewModel>.

I've to now bind the visibility of the views to some properties in the MainViewModel. I tried setting bool properties like IsContactViewSelected, IsNoteViewSelected using the setter of SelectedEntity property which is bound to the ListView's SelectedItem.

public SelectedEntity { //get set { oldvalue = _selectedEntity; _selectedEntity = value; // now i find the Type of model selected using oldvalue.ModelType // where ModelType is a property in the IntegratedViewModel // according to the type, i set one of the above bool properties to false // and do the same for _selectedEntity but set the property to true // so that the view corresponding to the selectedEntityType is visible // and others are collapsed } }

Here is the problem: For eg: let us say, I selected an item of type ContactModel, the old selection being NoteModel. I set the property IsNoteModelSelected to false according to the oldvalue, it sets the property and then Raises the propertychanged event and does not go and check the remaining if condition where i check for _selectedEntity which is used to set the IsContactModelSelected to true.

© Stack Overflow or respective owner

Related posts about c#

Related posts about wpf