我有一个longlistselector,其模板需要在应用栏按钮点击时更改。 appbar模板也会随按钮点击而更改。在.cs文件中使用按钮点击非常容易
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
}
tample都在page.resource里面。我正在尝试使用MVVM Light来使用MVVM模式。但是无法在ViewModel中创建命令我是mvvm的新手,
private void ApplicationBarIconButton_Click_1(object sender, EventArgs e)
{
var vm = (ViewModel)DataContext;
vm.SelectionCommand.Execute(null);
}
在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;
//}
});
有没有办法做到这一点,我试图在viewmodel中绑定longlistselector的Itemtemplate与字符串,但我认为不允许在WP8中绑定staticresource。请提供一些在Viewmodel中执行操作的方法。
AppBar Utils应该为你做,如果没有,它应该给你一些想法。 AppBar Utils允许您将不同的AppBars绑定到ViewModel上的值:
查看: