none
Mapping multiple instances of a view/viewmodel pair to different regions

    Question

  • Hi,

    I'm using prism/silverlight. I've created a view that has several components like datagrids,textboxes and buttons and it has a corresponding viewmodel. I need to map multiple instances of this view to different regions in my shell. For e.g., I want one instance of the view in one tab item, another instance in another tab item both of which independently map to independent view models instances. This is because the functionality is the same but they are different entities. How do i implement this?  

    Friday, December 10, 2010 3:40 AM

Answers

  • The above is Synchronized you have to create that with Asynchronized

    Class ViewModel : INotifyPropertyChanged

    private ListCollectionView _ArGroupCodes;
            public ListCollectionView ArGroupCodes
            {
                get
                {
                    if (_ArGroupCodes == null)
                    {
                        _ArGroupCodes = new ListCollectionView((new MozillaFXEntities()).ArGroupCode.ToList<ArGroupCode>());
                        //_ArGroupCodes = new ListCollectionView(new ObservableCollection<ArGroupCode>(dc.ArGroupCode));
                    }
                    return _ArGroupCodes;
                }
                set
                {
                    if (_ArGroupCodes != value)
                    {
                        _ArGroupCodes = value;
                        OnPropertyChanged("ArGroupCodes");
                    }
                }
            }

    http://forums.silverlight.net/forums/p/212764/502436.aspx#502436

    what is the problem now?

    Friday, December 10, 2010 6:09 AM

All replies

  • Hi,

    You can create that ViewModel with property with any entyties you would and then override DataContext match you control

    Regards

    Friday, December 10, 2010 4:07 AM
  • I dont understand. could you elaborate?

    Friday, December 10, 2010 4:14 AM
  • Do you have two tab items and two ViewModel use the same Function,your can do that just override DataContext

    DataContext="{Binding Current.Reference}" or you can create one ViewModel that feed the whole Views

    one ViewModel can contain multiple Entyties

    Command="{Binding StaticResources Key}"

    Friday, December 10, 2010 4:34 AM
  • I need 2 instances of the same viewmodel to map to 2 identical views in separate tab items

    Friday, December 10, 2010 4:40 AM
  • Hello,


    Look at http://msdn.microsoft.com/en-us/library/gg405494%28PandP.40%29.aspx

    Else you can use LifetimeManager if using unity else NonShared if using MEF for create on new instance each invoke

    Friday, December 10, 2010 4:49 AM
  • what am i looking for in the link? lifetimemanager looks promising.will check that out

    Friday, December 10, 2010 5:00 AM
  • Hi,

    Are You ViewModel just present Data Model or command too?

     

    Friday, December 10, 2010 5:09 AM
  • My viewmodel has an observablecollection that binds to a datagrid in my view

    Friday, December 10, 2010 5:16 AM
  • <usercontrol.DataContext>

    <you viewmodel/>

    </usercontrol.DataContext>

    <sdk:tab   DataContext="{Binding Customers}"    

    <sdk:tab   DataContext="{Binding CustSex}"    

     

    CSDomainContext Context = new CSDomainContext();

            private PagedCollectionView _customers;
            public PagedCollectionView Customers
            {
                get
                {
                    if (_customers == null)
                    {
                        _customers = new PagedCollectionView(Context.Customers);
                    }
                    return _customers;
                }
                set
                {
                    if (_customers != value)
                    {
                        _customers = value;
                    }
                }
            }

            private PagedCollectionView _custSex;
            public PagedCollectionView CustSex
            {
                get
                {
                    if (_custSex == null)
                    {
                        _custSex = new PagedCollectionView(Context.Customers);

                    }
                    return _custSex;
                }
                set
                {
                    if (_custSex != value)
                    {
                        _custSex = value;
                    }
                }
            }

    Thanks and Regards

    Friday, December 10, 2010 5:22 AM
  • A button in the view uses eventaggregator to add data to the datagrid. On click, it calls a function using 'subscribe'. But this function is set to add data to the first pagedcollection. How can i customize it to send the data to the corresponding datagrid?

    Also, isnt there a way I can just declare multiple instances and map them to their regions? This doesnt seem very scalable

    Friday, December 10, 2010 5:44 AM
  • Hi,

    You class have to impletement INotifyPropertyChaned interface

    Check this link http://forums.silverlight.net/forums/t/211765.aspx

    public abstract class ViewModelBase : INotifyPropertyChanged, IDisposable

    Regards

    Friday, December 10, 2010 5:56 AM
  • Isnt there someway I can directly declare multiple instances of the viewmodel? This doesnt seem very scalable

    Friday, December 10, 2010 6:01 AM
  • The above is Synchronized you have to create that with Asynchronized

    Class ViewModel : INotifyPropertyChanged

    private ListCollectionView _ArGroupCodes;
            public ListCollectionView ArGroupCodes
            {
                get
                {
                    if (_ArGroupCodes == null)
                    {
                        _ArGroupCodes = new ListCollectionView((new MozillaFXEntities()).ArGroupCode.ToList<ArGroupCode>());
                        //_ArGroupCodes = new ListCollectionView(new ObservableCollection<ArGroupCode>(dc.ArGroupCode));
                    }
                    return _ArGroupCodes;
                }
                set
                {
                    if (_ArGroupCodes != value)
                    {
                        _ArGroupCodes = value;
                        OnPropertyChanged("ArGroupCodes");
                    }
                }
            }

    http://forums.silverlight.net/forums/p/212764/502436.aspx#502436

    what is the problem now?

    Friday, December 10, 2010 6:09 AM