tags:

views:

200

answers:

2

How can I select a view for my viewmodel without using code behind? I want to load a ListBox with a lot of data coming from a WCF call. Using Josh Smith's app, the view loads before the data arrives, empty. I want to receive all the data and then select the view with spinner showing progress.

    // Completed Method
    void _backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        DataTemplate dataTemplate = new DataTemplate();
        //Leaderboards_All_View lb_all_view = new Leaderboards_All_View();
        CollectionViewSource collViewSrc = new CollectionViewSource();
        dataTemplate.DataType = "Leaderboards_All_View";
        List<Leaderboards_All> leaderboardList = e.Result as List<Leaderboards_All>;

        allViewModel =
            (from lbData in leaderboardList
             select new LB_Data_All_ViewModel(lbData, lb_All_ViewModel)).ToList();

        foreach (LB_Data_All_ViewModel avm in allViewModel)
            avm.PropertyChanged += this.OnLB_Data_All_ViewModelPropertyChanged;

        this.AllViewModel = new ObservableCollection<LB_Data_All_ViewModel>(allViewModel);
        this.AllViewModel.CollectionChanged += this.OnCollectionChanged;
        collViewSrc.Source = AllViewModel;
        lb_all_view.DataContext = collViewSrc;
    }
+1  A: 

-- How can I select a view for my viewmodel without using code behind?

Define your DataTemplates in xaml (probably in a ResourceDictionary, or in the Resources of the View, like (assuming you have declared 'vm', the namespace containing your ViewModels and 'vw', the namespace containing your Views):

<DataTemplate DataType="{x:Type vm:LB_Data_All_ViewModel}">
  <vw:LB_Data_All_View />
</DataTemplate>

HTH :)

IanR
A: 

Thank you, but I know how to set it in the XAML. I am asking how it can be selected PROGRAMMATICALLY in C#.

When the _backgroundWorker_RunWorkerCompleted is called, I want to select the view with C#. All the solutions I've seen involve changing the DataContext in the code behind, but I want to keep code out of code behind (which is the point of MVVM).

Josh Smith mentions "In more complex scenarios, it is possible to programmatically select the view, but in most situations that is unnecessary..." but he never gets around to providing an example. His demo app is very simplistic.


Nevermind. Don't everyone jump in at once! I found another post that had this link:

http://wildermuth.com/2009/05/22/Which%5Fcame%5Ffirst%5Fthe%5FView%5For%5Fthe%5FModel

Using a third class to "marry" the view and viewModel seems to be a rising consensus.

Gabriel
But you asked "How can I select a view for my viewmodel without using code behind?", which means in XAML.
Cameron MacFarland
"Programmatically" infers using C# not XAML, such as in the quote by Josh Smith: "In more complex scenarios, it is possible to programmatically select the view..." which I posted above. Not using code behind is the separation of the View and ViewModel which is fundemental to MVVM, decoupling the presentation from the logic.
Gabriel