Search code examples
c#xamlwindows-phone-8mvvmmvvm-light

Change template of app bar and long list selector from Viewmodel


I have a longlistselector whose template need to change on app bar button click. Also appbar template changes with button click too. inside .cs file of view it was pretty easy using button click

 private void ApplicationBarIconButton_Click_1(object sender, EventArgs e)
    {       
     DataTemplate dt;
            dt = (DataTemplate)this.Resources["NewTemplateSelection"];  //new template to apply
            list.ItemTemplate = dt;

           this.ApplicationBar = this.Resources["selectmodeAppBar"] as ApplicationBar;  //new 
template for app bar
}

both tample are inside page.resource. I am trying to use MVVM pattern using MVVM Light. But not able to create a command in ViewModel I am new to mvvm,

private void ApplicationBarIconButton_Click_1(object sender, EventArgs e)
    {      
        var vm = (ViewModel)DataContext;
        vm.SelectionCommand.Execute(null); 
      }

inside viewmodel

SelectionCommand = new RelayCommand(() =>
        {

           // DataTemplate dt;

           //  dt = (DataTemplate)  email.Resources["ViewEmailTemplateSelection"];
           //  email.list.ItemTemplate = dt;
           //  email.ApplicationBar.IsVisible = false;

           //email.ApplicationBar = email.Resources["selectmodeAppBar"] as ApplicationBar;
        //}
        });

is there a way to do it , I tried to bind Itemtemplate of longlistselector with string in viewmodel but i think it s not allowed to bind staticresource in WP8. Please suggest some way to do action in Viewmodel.


Solution

  • AppBarUtils should do it for you, if not, it should give you some ideas. AppBarUtils lets you bind different AppBars to a value on the ViewModel:

    Check out:

    AppBarUtils

    AppBarUtils (new home)

    Pro WP8 AppBar Management with AppBarUtils